That'll be, as Peter says, the For Each statements each need a Next statement. The error is saying 'Loop' isn't the right syntax to end the "For each s" statement.
Something like ...
Do while n<9
for each f in 'Territory_From$(n)_Comm1'
for each t in 'Territory_To$(n)_Comm1'
for each s in 'Sea_Sum$(n)_Comm1'
UniqueID&'.'&$(n) as LineID,
pick($(n),$(f)) as From,
pick($(n),$(t)) as To,
pick($(n),$(s)) as SeaSum
Where are you getting the error?
And what do you expect to get using the for each ... in loops?
You are missing NEXT statements for each FOR statement.
I don't understand your logic with the pick(), since each variable f,t,s only holds a single value a time and won't expand to a csv list, as pick() function expects.
Perhaps something like this works better:
UniqueID & '.' & Mid(FieldName, Index(FieldName,'_')-1,1) as LineID,
If(WildMatch(FieldName, 'Territory_From*'),Value) as From,
If(WildMatch(FieldName, 'Territory_To*)',Value) as To
If(WildMatch(FieldName, 'Sea_Sum*)',Value) as SeaSum,