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 <list>
#include <cstdio>

using namespace std;

int main(void){
    int n;
    char buf[1026], *p;
    list<char> trains;
    list<char>::iterator it;
    
    scanf("%d", &n);
    for (int i=0; i<n; i++){
        trains.clear();
        scanf("%s", buf);
        p = buf;
        trains.push_front(*p++);
        it = trains.begin();
        while (1){
  //          printf("(deb:%c)\n", *it);
            if (*p=='<'){
                p+=2;
                if (it==trains.begin()){
  //                  puts("deb:pf");
                    trains.push_front(*p);
                }
                p++;
                it--;
            }
            else if(*p=='-'){
                p+=2;
                it++;
                if (it==trains.end()){
  //                  puts("deb:pb");
                    it = trains.insert(it, *p);
                }
                p++;
            }
            else break;
        }
        for (it = trains.begin(); it!=trains.end(); it++){
            putchar(*it);
        }
        puts("");
    }
    return 0;
}

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

typedef string::iterator S;

struct Node{
    char id;
    Node* prev;
    Node* next;
};

int main(){
    int n,i;
    cin>>n;
    while(n--){
        string expr;
        cin>>expr;
        Node vs[26];
        for(i=0;i<26;i++){
            vs[i].id=0;
            vs[i].prev=NULL;
            vs[i].next=NULL;
        }
        Node* vp = vs;
        Node* fr = vp+1;
        Node* head = vp;
        S s = expr.begin();
        while( true ){
            vp->id = *s;
            s++;
            if( s == expr.end() ) break;
            if( *s == '-' ){
                if( vp->next == NULL ){
                    vp->next = fr;
                    (vp->next)->prev = vp;
                    fr++;
                }
                vp = vp->next;
            }else{
                if( vp->prev == NULL ){
                    vp->prev = fr;
                    (vp->prev)->next = vp;
                    fr++;
                    head = vp->prev;
                }
                vp = vp->prev;
            }
            s += 2;
        }
        while( head ){
            cout << head->id;
            head = head->next;
        }
        cout << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <climits>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef vector<int> VI;
typedef vector< VI > VVI;

typedef long long LL;

typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define X first
#define Y second

const char NONE = '0';

int main(void) {
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		string str;
		cin >> str;

		string train(500, NONE);
		int pos = 250;
		train[pos] = str[0];

		for (int j = 1; j < (int)str.size(); j += 3) {
			char c = str[j + 2];
			int dir = str[j] == '<' ? -1 : 1;
			pos += dir;
			train[pos] = c;
		}

		remove_if(train.begin(), train.end(), [](char c){ return c == NONE; });
		train = train.substr(0, train.find(NONE));

		cout << train << endl;
	}

	return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define LINEMAX 1025
char train[60];

typedef struct _two {
	int head;
	int tail;
} TWO_T;

TWO_T maketrain( char str[LINEMAX] )
{
	int medium_stat = 30;
	TWO_T tval;
	tval.head = 30; tval.tail = 30;
	char *p; p = str;

	// 一番最初の文字列が列車の最初の車両としてスタート, 
	// 左・右に進むにつれて列車の構成を変更していく
	while ( *p ) {
		if ( isalpha( *p ) ) {
			train[medium_stat] = *p;
			p++;
		} else { 
			if ( *p == '-' ) {
				medium_stat++;
				if ( medium_stat > tval.tail ) {
					tval.tail = medium_stat;
				}
			} else if ( *p == '<' ) {
				medium_stat--;
				if ( medium_stat < tval.head ) {
					tval.head = medium_stat;
				}
			}
			p += 2;
		}
	}
	return tval;
}


int main()
{
	char str[LINEMAX];
	int num, midium_stat = 30;
	TWO_T tval;

	scanf( "%d", &num );

	for ( int i = 0 ; i < num ; i++ ) {
		memset( train, '0', sizeof( train ) );
		scanf( "%s", str );
		tval = maketrain( str );
		for ( int j = tval.head ; j <= tval.tail ; j++ ) {
			printf( "%c", train[j] );
		}
		printf( "\n" );
	}
	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(260, -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;
  }
}