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<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
vector<int>g[26];
vector<int>ans;
bool used[26];
void dfs(int pos){
    if(used[pos])return;
    used[pos]=true;
    for(int i=0;i<g[pos].size();i++){
        dfs(g[pos][i]);
    }
    ans.push_back(pos);
}
int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        fill_n(g,26,vector<int>(0));
        fill_n(used,26,false);
        ans.clear();
        char str[1025];
        scanf("%s",str);
        for(int i=1;str[i];i+=3){
            int v1=str[i-1]-'a';
            int v2=str[i+2]-'a';
            if(str[i]=='-'){
                g[v1].push_back(v2);
            }
            else{
                g[v2].push_back(v1);
            }
        }
        for(int i=0;i<26;i++){
            sort(g[i].begin(),g[i].end());
            g[i].erase(unique(g[i].begin(),g[i].end()),g[i].end());
        }
        for(int i=0;i<26;i++)if(g[i].size())dfs(i);
        reverse(ans.begin(),ans.end());
        for(int i=0;i<ans.size();i++){
            printf("%c",ans[i]+'a');
        }
        puts("");
    }

    return 0;
}

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

using namespace std;

int main(){
	
	int n;
	
	string str;
	
	char c[100];
	
	cin >> n;
	
	for(int loop = 0; loop < n; loop++){
		cin >> str;
		
		if(cin.eof()){
			break;
		}
		
		for(int i = 0; i < 100; i++){
			c[i] = ' ';
		}
		
		int index = 50;
		
		for(int i = 0; i < str.size(); i += 3){
			c[index] = str[i];
			
			if(i == str.size()){
				break;
			}
			
			if(str[i + 2] == '>'){
				index++;
			}else{
				index--;
			}
		}
		
		for(int i = 0; i < 100; i++){
			if(c[i] != ' '){
				printf("%c", c[i]);
			}
		}
		printf("\n");
	}
	
	return 0;
}

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

int main()
{
    int n;
    string s;
    cin >> n;
    getline(cin, s);
    for(int i = 0; i < n; i++)
    {
        string ans;
        string in_str;
        bool is_appeared[26] = {};
        std::getline(cin, in_str);
        ans.push_back(in_str[0]);
        is_appeared[in_str[0] - 'a'] = true;
        for(int j = 1; j < in_str.length() - 3; j++)
        {
            if(is_appeared[in_str[j + 2] - 'a'])
            {
                continue;
            }
            else
            {
                is_appeared[in_str[j + 2] - 'a'] = true;
            }

            if(in_str[j] == '-' && in_str[j + 1] == '>')
            {
                ans.push_back(in_str[j + 2]);
            }
            else if(in_str[j] == '<' && in_str[j + 1] == '-')
            {
                ans.insert(ans.begin(), in_str[j + 2]);
            }
            else
            {
                /* Nop */
            }
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j;
  int len,n;
  vector<string> u;
  list<string> a;
  cin>>n;
  for(i=0;i<n;i++){
    string s;
    cin>>s;
    u.push_back(s);
  }
  for(i=0;i<n;i++){
    a.clear();
    a.push_back(u[i].substr(0,1));
    len=u[i].length();
    for(j=1;j<len;j+=3){
      if(u[i][j+1]=='>'){
	if(u[i].substr(j-1,1)==a.back())
	  a.push_back(u[i].substr(j+2,1));
      }else if(u[i][j]=='<'){
	if(u[i].substr(j-1,1)==a.front())
	  a.push_front(u[i].substr(j+2,1));
      }
    }
    list<string>::iterator it=a.begin();
    for(;it!=a.end();it++)
      cout<<*it;
    cout<<endl;
  }
  return 0;
}

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 <cstdio>
#include <queue>
using namespace std;

int main()
{
	int n;
	
	scanf("%d", &n);
	while (n-- > 0){
		char str[2048] = {0};
		scanf("%s", str);
		
		deque<char> train;
		train.push_back(*str);
		bool v[26] = {false};
		v[*str - 'a'] = true;
		for (int i = 3; str[i] != 0; i += 3){
			if (!v[str[i] - 'a']){
				if (str[i - 1] == '>') train.push_back(str[i]);
				else train.push_front(str[i]);
				v[str[i] - 'a'] = true;
			}
		}
		
		while (!train.empty()){
			putchar(train.front());
			train.pop_front();
		}
		puts("");
	}
	
	return 0;
}

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

using namespace std;

int main() {

  int n;
  cin >> n;

  for(int q=0;q<n;q++) {
    string str;
    cin >> str;

    string train;

    train.clear();
    int ima = 0;
    int head = 0,tail=0;
    train += str[0];
    for(int i=0;i<str.size();i++) {

      if(str[i] == '>' ) {
	ima++;
	if(head < ima) {
	train += str[i+1];
	head = ima;
	}
      }
      else if(str[i] == '<'){
	ima--;
	if(tail > ima) {
	  tail = ima;
	  reverse(train.begin(),train.end());
	  train += str[i+2];
	  reverse(train.begin(),train.end());
	}
      }

    }
    
    cout << train << endl;

  }

  return 0;

}