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 <list>

using namespace std;

int main()
{
	int n;
	char trains[2048];

	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		char train_bucket[256] = {0};
		list<char> ans;

		scanf(" %s", trains);
		
		ans.push_front(*trains);
		train_bucket[*trains] = 1;
		for (int ti = 3; trains[ti] != '\0'; ti++){
			if (!train_bucket[trains[ti]]){
				train_bucket[trains[ti]] = 1;
				if (trains[ti - 1] == '>') ans.push_back(trains[ti]);
				else ans.push_front(trains[ti]);
			}
			if (trains[ti + 1] != '\0') ti += 2; 
		}

		for (list<char>::iterator it = ans.begin(); it != ans.end(); it++){
			printf("%c", *it);
		}
		puts("");
	}

	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;
    int n;
    cin >> n;
    while (n--) {
	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 <algorithm>
#include <list>
using namespace std;

int main() {
  int n;
  cin >> n; cin.ignore();
  
  while(n--) {
    string str;
    cin >> str;
    list<char> li;
    list<char>::iterator it;
    inserter( li, li.end() ) = str[0];
    for(int i=3; i<str.size(); i+=3) {
      if(find(li.begin(), li.end(), str[i]) != li.end())
	continue;
      it = find(li.begin(), li.end(), str[i-3]);
      if(str[i-1] == '>')
	inserter( li, ++it ) = str[i];
      else
	inserter( li, it ) = str[i];
    }
    
    for(it = li.begin(); it != li.end(); it++ ) {
      cout << *it;
    }
    cout << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
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-1;i--)cout<<a[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 forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
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 ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int &s = 0, const int &d = 0, const Weight &w = 0) : src(s), dst(d), weight(w) {}
};

using Edges = std::vector<Edge>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

class Graph {
  std::vector<Edges> g;
  using iterator = std::vector<Edges>::iterator;
  using const_iterator = std::vector<Edges>::const_iterator;

public:
  Graph(const int &size = 0) : g(size) {}
  int size() const { return g.size(); }
  const Edges &operator[](const int &i) const { return g[i]; }
  void addArc(const int &src, const int &dst, const Weight &w = 1) { g[src].emplace_back(src, dst, w); }
  void addEdge(const int &node1, const int &node2, const Weight &w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }
  iterator begin() { return g.begin(); }
  const_iterator begin() const { return g.begin(); }
  iterator end() { return g.end(); }
  const_iterator end() const { return g.end(); }
};

std::vector<int> tsort(const Graph &g) {
  int n = g.size(), k = 0;
  std::vector<int> ord(n), in(n);
  for (auto &es : g) {
    for (auto &e : es) in[e.dst]++;
  }
  std::queue<int> q;
  rep(i, n) if (in[i] == 0) q.push(i);
  while (q.size()) {
    int v = q.front();
    q.pop();
    ord[k++] = v;
    for (auto &e : g[v])
      if (--in[e.dst] == 0) q.push(e.dst);
  }
  return *std::max_element(in.begin(), in.end()) == 0 ? ord : std::vector<int>();
}

main {
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    if (s.size() == 1) {
      cout << s << endl;
      continue;
    }
    set<char> a;
    Graph g(26);
    rep(i, s.size() / 3) {
      a.insert(s[i * 3]);
      a.insert(s[i * 3 + 3]);
      if (s[i * 3 + 1] == '-') {
        g.addArc(s[i * 3] - 'a', s[i * 3 + 3] - 'a');
      } else {
        g.addArc(s[i * 3 + 3] - 'a', s[i * 3] - 'a');
      }
    }
    vi v = tsort(g);
    for (auto &x : v) {
      if (a.count(x + 'a')) cout << char(x + 'a');
    }
    cout << endl;
  }
}

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

int main() {
	int n ; cin >> n ; cin.ignore();
	while(n--)
	{
		string str; getline(cin,str);
		deque<char> dq; deque<char>::iterator ite;
		char ch; ch=str[0]; dq.push_front(ch);
		ite=dq.begin();
		for(int i=1;i<str.size();i+=3)
		{
			string tmp="";
			tmp+=str[i]; tmp+=str[i+1];
			ch=str[i+2];
			if(tmp=="->")
			{
				ite++;
				if(ite==dq.end()) dq.push_back(ch);
				else if(*ite!=ch) dq.insert(ite,ch);
			}
			else
			{
				if(ite==dq.begin()) dq.push_front(ch),ite--;
				else
				{
					ite--;
					if(*ite!=ch) dq.insert(ite,ch);
				}
			}
		}
		for(ite=dq.begin();ite<dq.end();ite++) cout << *ite ;
		cout << '\n' ; dq.clear();
	}
}