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<string>
#include<deque>
#include<cstring>

int main()
{
	int n;
	std::cin >> n;
	
	while( n-- )
	{
		std::string s;
		std::cin >> s;

		std::deque<char> deq;
		deq.push_back( s[0] );
		
		int p = 0;
		
		for( int i = 1; i < s.size(); i += 3 )
		{
			if( s[i] == '<' && !(p--) )
				deq.push_front( s[i+2] ), ++p;
			else if( s[i+1] == '>' && ++p == deq.size() )
				deq.push_back( s[i+2] );
		}

		for( auto it = deq.begin(); it != deq.end(); ++it )
			std::cout << *it;

		std::cout << std::endl;
	}

	return 0;
}

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

using namespace std;

int main(int argc, char const* argv[])
{
	string input;
	char answer[64];
	int n;

	cin >> n;

	for( int i = 0;i < n;i++ ){
		cin >> input;
		for( int j = 0;j < 64;j++ ) answer[j] = '\0';
		int pos = 32;

		answer[pos] = input[0];
		for( int j = 3;j < input.size();j += 3 ){
			if( input[j - 2] == '<' ){
				pos--;
				answer[pos] = input[j];
			} else if( input[j - 2] == '-' ){
				pos++;
				answer[pos] = input[j];
			}
		}

		int j;
		for( j = 0;j < 64;j++ ){
			if( answer[j] != '\0' ) break;
		}

		cout << ( answer + j ) << endl;
	}
				

	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

bool issame (deque<char> train, char c )
{
	deque<char>::iterator it = train.begin();
	for (; it != train.end(); ++it){
		if ((*it) == c ){
			return true;
		} // end if
	} // end for

	return false;
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.train", "r", stdin );
	string str = "";
	int n;

	while (getline (cin, str ) ){
		stringstream ssn(str);
		ssn >> n;
		if (n == 0 ){
			break;
		} // end if
		int i,j;
	
		for (i = 0; i < n; ++i){

			deque<char> train;

			getline (cin, str );

			int len = str.length();
			str += 'E';
			train.push_back(str[0]);
			
			for (j = 1; j < len - 1; ++j){
				if (str[j] != '<' && str[j] != '>' && str[j] == 'E'){
					continue;
				} // end if
				if (str[j] == '>'){
					if (!issame(train, str[j+1] ) ) {
						train.push_back(str[j+1]);
					} // end if
				}else if (str[j] == '<'){
					if (!issame(train, str[j+2] ) ) {
						train.push_front(str[j+2]);
					} // end if
				} // end if
			} // end for

			// train output
			deque<char>::iterator it = train.begin();
			for (; it != train.end(); ++it){
				cout << (*it);
			} // end for
			cout << endl;
			train.clear();
		} // end for

	} // end loop
		
	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');
    // }
    int i = distance(f.begin(), find(all(f), 1));
    for (;;) {
      cout << char(i + 'a');
      i = t[i];
      if (i == -1) break;
    }
    cout << endl;
  }
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <unordered_map>

using namespace std;
using LL = long long;

constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;

int main() {
	int n;
	cin >> n;
	for (int o = 0; o < n; o++) {
		string s;
		cin >> s;
		string ans;
		bool count[1000];
		for (int i = 0; i < 1000; i++)count[i] = false;
		ans += s[0];
		count[s[0]] = true;
		for (int i = 1; i < s.size(); i += 3) {
			if (s[i + 1] == '>' && !count[s[i + 2]])ans = ans + s[i + 2], count[s[i + 2]] = true;
			else if (s[i] == '<' && !count[s[i + 2]])ans = s[i + 2] + ans, count[s[i + 2]] = true;
		}
		cout << ans << endl;
	}
}

C++: #include "bits/stdc++.h"
using namespace std;
using namespace std;
 
int main(){
    int n,cnt;
    string s;
    cin >> n;
    while(n--){
        map <int,char> data;
        map <int,char>::iterator itr;
        cnt = 0;
        cin >> s;
        for(int i=0;i<s.length();i++){
            if(isalpha(s[i])){
                data.insert(map<int,char>::value_type(cnt,s[i]));
            }
            else if(s[i] == '<') cnt--;
            else if(s[i] == '>') cnt++;
        }
        for(itr=data.begin();itr!=data.end();itr++){
            cout << itr->second;
        }
        cout << endl;
    }
}