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++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define PB push_back
#define MP make_pair
int main(){
  string s;
  int n;
  while(cin>>n){
    while(n--){
      char train[61];
      for(int i=0;i<61;i++)train[i]='*';
      cin>>s;
      int pos=30;
      train[pos]=s[0];
      for(int i=3;i<s.size();i+=3){
        if(s[i-1]=='-')pos--;
        else pos++;
        train[pos]=s[i];
      }
      for(int i=0;i<61;i++){
        if(train[i]!='*'){
          while(1){
            cout<<train[i];
            i++;
            if(train[i]=='*')break;
          }
          cout<<endl;
          break;
        }
      }
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define fr first
#define sc second
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> pip; 
const int INF = (1<<25);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main(){

  int n;
  int data[30][2];
  int now_train;

  cin>> n;
  while(n--){
    for(int i=0;i<30;i++) data[i][0]=data[i][1]=-1;
    char c;
    cin>> c;
    now_train=abs('a'-c);
    
    string s;
    cin>> s;
    for(int i=0;i<s.size();i+=3){
      if(s[i]=='-'){
	data[now_train][1]=abs('a'-s[i+2]);
	data[abs('a'-s[i+2])][0]=now_train;
      }
      else{
	data[now_train][0]=abs('a'-s[i+2]);
	data[abs('a'-s[i+2])][1]=now_train;
      }
      now_train=abs('a'-s[i+2]);
    }
    
    string ans;
    int left,right;
    for(int i=0;i<26;i++){
      if(data[i][0]==-1 && data[i][1]!=-1){
	left=i;
	ans+='a'+left;
      }
      else if(data[i][1]==-1 && data[i][0]!=-1){
	right=i;
      }
    }
    while(left!=right){
      ans+='a'+data[left][1];
      left=data[left][1];
    }
    cout<< ans<< endl;
  }
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  cin >> n;
  string gomi;
  cin.ignore();
  while (n--) {
    string s;
    getline(cin, s);
    vc a;
    vs b;
    rep(i, s.size() / 3) {
      a.pb(s.substr(i * 3, 1)[0]);
      b.pb(s.substr(i * 3 + 1, 2));
    }
    a.pb(s.back());
    vi t(26, -1), f(26);
    loop(i, 1, a.size()) {
      if (b[i - 1] == "->") {
        t[a[i - 1] - 'a']                              = a[i] - 'a';
        if (f[a[i - 1] - 'a'] != -1) f[a[i - 1] - 'a'] = 1;
        f[a[i] - 'a']                                  = -1;
      } else {
        t[a[i] - 'a']                          = a[i - 1] - 'a';
        if (f[a[i] - 'a'] != -1) f[a[i] - 'a'] = 1;
        f[a[i - 1] - 'a']                      = -1;
      }
    }
    for (int i = distance(f.begin(), find(all(f), 1)); i != -1; i = t[i]) {
      cout << char(i + 'a');
    }
    cout << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#include <map>
#include <list>
#include <set>
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    while(n--)
    {
        string s; cin >> s;
        list<char> ls;
        set<char> st;
        ls.push_back(s[0]);
        st.insert(s[0]);
        for(int i = 1; i < s.size(); i += 3)
        {
            string t = s.substr(i, 2);
            if(st.count(s[i + 2])) continue;
            if(t == "->") ls.push_back(s[i + 2]);
            else ls.push_front(s[i + 2]);
            st.insert(s[i + 2]);
        }
        for(auto x : ls) cout << x;
        cout << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>

int main(){
	int n;
	std::cin >> n;
	for (int i = 0; i < n; i++){
		char train[60];
		for (int j = 0; j < 60; j++)train[j] = '?';
		int it = 30;

		std::string str;
		std::cin >> str;

		train[it] = str[0];
		for (int j = 1; j < str.size(); j++){
			if (str[j] == '>'){
				it++;
				if (train[it] != str[j + 1])train[it] = str[j + 1];
			}
			else if (str[j] == '<'){
				it--;
				if (train[it] != str[j + 2])train[it] = str[j + 2];
			}
		}

		for (int j = 0; j < 60; j++){
			if (train[j] != '?')std::cout << train[j];
		}
		std::cout << std::endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  cin >> n;
  while(n--){
    string str; cin >> str;
    map<int,char> data;
    map<int,char>::iterator itr;
    int cnt = 0;
    for(int i=0;i<str.size();i++){
      if(isalpha(str[i])) data[cnt] = str[i];
      else if(str[i] == '<') cnt--;
      else if(str[i] == '>') cnt++;
    }
    for(itr = data.begin();itr!=data.end();itr++){
      cout << (*itr).second;
    }
    cout << endl;
    data.clear();
  }
}