Go: package main

import (
	"bufio"
	"os"
	"unicode"
	"strconv"
	"fmt"
	"strings"
	)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s := strings.Split(scanner.Text(), "")
		ans, dir := make([]string, 0), ">"
		for _, x := range s {
			if IsLetter(x) {
				if dir == ">" {
					if !isIn(ans, x) {
						ans = append(ans, x)
					}
				} else {
					if !isIn(ans, x) {
						ans = append([]string{x}, ans...)
					}
				}
			} else if x == ">" || x == "<" {
				dir = x
			}
		}
		fmt.Println(strings.Join(ans, ""))
	}
}

func IsLetter(s string) bool {
	for _, r := range s {
		if !unicode.IsLetter(r) {
			return false
		}
	}
	return true
}

func isIn(arr []string, target string) bool {
	for _, val := range arr {
		if val == target {
			return true
		}
	}
	return false
}

C++: //Name: Train
//Level: 2
//Category: シミュレーション
//Note: 

/**
 * stringでシミュレーションする。
 *
 * オーダーは O(|S|^2)。
 */
#include <iostream>
#include <string>

using namespace std;

bool solve(bool first) {
    string s;
    cin >> s;

    string res;
    res += s[0];
    string::iterator it = begin(res);
    for(int i = 2; i < s.size(); i += 3) {
        bool top = false;
        if(s[i] == '>') {
            ++it;
        } else if(s[i] == '-') {
            if(it == begin(res)) top = true;
            else --it;
        }
        if(top || it == end(res)) {
            it = res.insert(it, s[i+1]);
        }
    }
    cout << res << endl;
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(10);

    bool first = true;
    int N;
    cin >> N;
    while(N-- && solve(first)) {
        first = false;
    }
    return 0;
}

C++: #include<iostream>
#include<deque>
#include<string>
using namespace std;

int main(){
	int n;
	cin>>n;
	while(n--){
		deque<char> deq;	
		string s;
		cin>>s;
		int p=0;
		deq.push_front(s[0]);
		for(int i=1;i<s.size();i+=3){
			if(s[i]=='<'){
				if(p==0)deq.push_front(s[i+2]);
				else p--;
			}else{
				if(p+1==deq.size())deq.push_back(s[i+2]);
				p++;
			}
		}
		for(int i=0;i<deq.size();i++)cout<<deq[i];
		cout<<endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
char s[1050];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%s",s);
		int now=0;
		int cnt=0;
		int l=0,r=0;
		deque<char> ans;
		int sz=strlen(s);
		bool fir=true;
		while(cnt<sz)
		{
			if(fir)
			{
				fir=false;
				ans.push_front(s[0]);
				if(s[1]=='-')now++;
				else now--;
				cnt+=3;
				continue;
			}
			if(now>r)
			{
				ans.push_back(s[cnt]);
				//cout << "push back " << s[cnt] << endl;
				r=now;
			}
			if(now<l)
			{
				ans.push_front(s[cnt]);
				//cout << "push front " << s[cnt] << endl;
				l=now;
			}
			if(cnt+1==sz)break;
			if(s[cnt+1]=='-')now++;
			else now--;
			cnt+=3;
		}
		for(deque<char>::iterator it=ans.begin();it!=ans.end();it++)
		{
			cout << *it;
		}
		cout << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int c (string str, int dir, int cursor, char a) {
    for (int i = cursor; 0 <= i && i < str.size(); i += dir) {
        if (a == str[i]) {
            return i;
        }
    }

    return cursor + dir;
}

int main ()
{
    string str;
    while (cin >> str) {
        int cursor = 0;
        string res = "";

        res += str[0];

        for (int i = 1; i < str.size()-2; i += 3) {
            if (str[i] == '-') {
                cursor = c(res, 1, cursor, str[i+2]);
                if (res.size() == cursor) {
                    res += str[i+2];
                }
            } else if (str[i] == '<') {
                cursor--;
                cursor = c(res, -1, cursor, str[i+2]);
                if (cursor < 0) {
                    res = str[i+2] + res;
                    cursor = 0;
                }
            }
        }

        cout << res << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <climits>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector< VI > VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

const char NONE = '0';

int main(void) {
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		string str;
		cin >> str;

		string train(100, NONE);
		int pos = 50;
		train[pos] = str[0];

		for (int j = 1; j < (int)str.size(); j += 3) {
			char c = str[j + 2];
			int dir = str[j] == '<' ? -1 : 1;
			pos += dir;
			train[pos] = c;
		}

		remove_if(train.begin(), train.end(), [](char c){ return c == NONE; });
		train = train.substr(0, train.find(NONE));

		cout << train << endl;
	}

	return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EACH(t,i,c) for(t::iretator i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,n-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
 
int main() {
	int N;
	cin >> N;

	REP(i, N) {
		string s;
		map<int, char> table;
		int mi = 0, ma = 0, pos = 0;

		cin >> s;

		REP(j, s.size()) {
			if(s[j] == '-') {

			} else if(s[j] == '<') {
				pos--;
				mi = min(mi, pos);
			} else if(s[j] == '>') {
				pos++;
				ma = max(ma, pos);
			} else {
				table.insert(map<int, char>::value_type(pos, s[j]));
			}
		}

		FOR(j, mi, ma) {
			cout << table[j];
		}
		cout << endl;
	}
	
	return 0;
}