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>
using namespace std;

int main()
{
  int n;
  cin >> n;
  char cars[51];
  for (int dataset =0; dataset<n; dataset++)
  {
    int now_car = 25;
    int first_car = 25;
    int end_car = 25;
    bool flag = true;
    char op;
    while(flag)
    {
      char car;
      cin >> car;
      if ( car == '-' || car == '<' )
      {
        op = car;
      }
      else 
      {
        cars[now_car] = car;
        cin >> op;
      }
      if ( cin.eof() )
      {
        break;
      }
      switch (op)
      {
      case '<':     //  前方への移動
        now_car--;
        if ( now_car < first_car )
        {
          first_car = now_car;
        }
        cin >> op;    //  矢印をひとつ捨てる
        break;
      case '-':
        now_car++;
        if ( end_car < now_car )
        {
          end_car = now_car;
        }
        cin >> op;    //  矢印をひとつ捨てる
        break;
      default:
        flag = false;
        break;
      } //switch(op)
    } //while(flag)

    for (int i=first_car; i<=end_car; i++)
    {
      cout << cars[i] << flush;
    }
    cout << endl;
    cars[25] = op;
  } //for
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int cn;
char t[51];
string s;

int main(){
	cin>>cn;
	while(cin>>s){
		fill(t,t+51,0);
		int cur=0,n=25;
		while(1){
			t[n]=s[cur++];
			if(cur==s.size())break;
			if(s[cur]=='-')n++;
			else n--;
			cur+=2;
		}
		for(int i=0;i<51;i++)if(t[i])cout<<t[i];
		cout<<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 <bits/stdc++.h>
using namespace std;

int main() {
  int T;
  cin >> T;
  while(T--) {
    string s,ans="";
    int k=0;
    cin >> s;
    ans=s[0];
    for(int i=3; i<s.size(); i+=3) {
      if(s[i-1]=='>') k++;
      else k--;
      if(k==ans.size()) ans+=s[i];
      if(k==-1) {
        ans=s[i]+ans;
        k++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define R(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
  int n;
  cin>>n;
  while(n--){
    int i,k=1500,m2=10000,m1=0;
    char a[3000];
    string s;
    cin>>s;
    a[k]=s[0];
    for(i=1;i<s.size();i+=3){
      if(s[i]=='-')k--;
      else k++;
      a[k]=s[i+2];
      m1=max(m1,k);
      m2=min(m2,k);
    }
    for(i=m1+1;i>=m2;i--)cout<<a[i];
      cout<<endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <map>

using namespace std;

int main() {
  int N; cin >> N;
  for (int i = 0; i < N; i++) {
    map<int, char> m;
    int start = 0;
    int end = 0;
    int pos = 0;
    string train;
    cin >> train;
    m[0] = train[0];
    for (unsigned int i = 1; i < train.size(); i+=3) {
      char c = train[i+2];
      if (train[i+1] == '>') pos++;
      else pos--;
      m[pos] = c;
      if (pos < start) start = pos;
      if (end < pos) end = pos;
    }
    for (int i = start; i <= end; i++) {
      cout << m[i];
    }
    cout << endl;
  }


  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <fstream>
#include <sstream>
#include <math.h>
#include <queue>
#include <stack>
#include <math.h>


using namespace std;




int main(){
    int n;
    while(cin>>n){
        string input;
        for(int roop=0; roop<n; roop++){
            cin>>input;
            bool go_end = true;
            int now=0;
            int len=0;
            string out_put;
            for(int i=0; i<input.length(); i++){
                if(input[i]=='-')continue;
                else if(input[i]=='<'){
                    now--;
                    go_end=false;
                }
                else if(input[i]=='>'){
                    go_end=true;
                    now++;

                }
                else{
                    if(go_end&&now==len){
                        out_put+=input[i];
                        len++;
                    }else if(!go_end&&now==-1){
                        out_put=input[i]+out_put;
                        len++;
                        now++;
                    }
                }
            }
            cout<<out_put<<endl;

        }
        
    }
}

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

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

		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 < 30; j++){
			if (train[j] != '?')std::cout << train[j];
		}
		std::cout << std::endl;
	}
	return 0;
}