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>

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

		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;
}

C++: # define _CRT_SECURE_NO_WARNINGS 1
# include <iostream>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <map>
# include <stack>
using namespace std;
//# define M_PI 3.141592
# define INF ((int)(1<<30))
# define FOR(i,n) for(int i=0;i<(int)n;i++)
# define FORI(i,k,n) for(int i=k;i<(int)n;i++)
# define toRad 2.0*M_PI/360.0
# define inin(x) int x;cin>>x;
# define all(x) x.begin(),x.end()
# define debug(x) cout<<#x<<" :"<<x<<endl;
# define rep(i,n) for(int i=0;i<(int)n;i++)
# define EPS 1e-12
# define CHECK(i,a) FOR(i,a.size())cout<<#a<<"["<<i<<"] : "<<a[i]<<endl; 

class Point
{
public:
	double x, y;

	Point(double x = 0, double y = 0) :x(x), y(y) {}


	Point operator + (Point p) { return Point(x + p.x, y + p.y); }
	Point operator - (Point p) { return Point(x - p.x, y - p.y); }
	Point operator * (double k) { return Point(x * k, y * k); }
	Point operator / (double k) { return Point(x / k, y / k); }
	bool operator < (const Point &p)const { return x != p.x ? x < p.x : y < p.y; }
	bool operator ==(const Point &p)const { return fabs(x - p.x) < EPS&&fabs(y - p.y) < EPS; }

	double abs() { return sqrt(norm()); }
	double norm() { return x*x + y*y; }
};
typedef Point Vector;
struct Segment
{
	Point p1, p2;
};
typedef Segment Line;
class Circle
{
public:
	Point c;
	double r;
	Circle(Point c = Point(), double r = 0.0) :c(c), r(r) {}
};

double dot(Vector a, Vector b) { return a.x*b.x + a.y*b.y; }//??????
double cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x; }//??????
bool isOrthogonal(Vector a, Vector b) { return fabs(dot(a, b)) < EPS; }//??´??????
bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) { return isOrthogonal(a1 - a2, b1 - b2); }
bool isOrthogonal(Line l1, Line l2) { return fabs(dot(l1.p2 - l1.p1, l2.p2 - l2.p1)) < EPS; }
bool isParallel(Vector a, Vector b) { return fabs(cross(a, b)) < EPS; }//?????????
bool isParallel(Point a1, Point a2, Point b1, Point b2) { return isParallel(a1 - a2, b1 - b2); }
bool isParallel(Line l1, Line l2) { return fabs(cross(l1.p2 - l1.p1, l2.p2 - l2.p1)) < EPS; }
Point project(Line s, Point p)
{
	Vector base = s.p2 - s.p1;
	double r = dot(p - s.p1, base) / base.norm();
	return s.p1 + base*r;
}



int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		string a;
		cin >> a;
		vector<char> v;
		v.push_back(a[0]);
		auto it = v.begin();
		for (int i = 2; i < a.size();i+=3)
		{
			if (a[i] == '>')
			{
				it++;
				if (it == v.end())it=v.insert(it, a[i + 1]);
			}
			else
			{
				if(it==v.begin())it = v.insert(v.begin(), a[i + 1]);
				else it--;
			}
		}
		for (int i = 0; i < v.size(); i++)
		{
			cout << v[i];
		}
		cout << endl;

	}
}

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

int main()
{
	int n,siz,ite,strite;
	string res,str;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> str;
		res = str[0];
		strite = 1;
		siz = 0;
		ite = 0;
		while (strite < str.size()) {
			if (str[strite] == '<') {
				if (ite == 0) {
					siz++;
					strite += 2;
					res = str[strite++] + res;
				} else {
					strite += 3;
					ite--;
				}
			}
			else if (str[strite] == '-') {
				if (ite == siz) {
					siz++;
					ite = siz;
					strite += 2;
					res += str[strite++];
				} else {
					strite += 3;
					ite++;
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <map>
int main(){
	int n;
	std::cin >> n;
	std::string s;
	while(n--, std::cin >> s){
		int pos = 0;
		while(pos = s.find("-", pos), pos != std::string::npos){
			s.replace(pos, 1, "");
		}

		std::map<char, char> m, rm;
		for(int i=1;i<s.size();i+=2){
			if(s[i] == '>')
				m[s[i+1]] = s[i-1], rm[s[i-1]] = s[i+1];
			else
				m[s[i-1]] = s[i+1], rm[s[i+1]] = s[i-1];
		}

		char end = (*m.begin()).first;
		while(m[end] != 0){
			end = m[end];
		}
		//whileI¹_ÅendÍstartÆÈé

		while(rm[end] != 0){
			std::cout << end;
			end = rm[end];
		}
		std::cout << end << '\n';
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VII;
typedef pair<int, int> PII;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back;
#define MP make_pair
#define SIZE(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 3;

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

string solve(string &input){
  string train(input, 0, 1);
  int f = 0, b = 0, n = 0;
  for(int i = 1; i < input.size(); i++){
    if(input[i] == '<') n++;
    else if(input[i] == '>') n--;
    else if(input[i] != '-' && n > f) { train.insert(train.begin(), input[i]); f = n;}
    else if(input[i] != '-' && n < b) { train.insert(train.end(), input[i]); b = n;}
  }
  return train;
}

int main(void){
  int n;
  cin >> n;
  for(int i = 0; i < n; i++){
    string input;
    cin >> input;
    cout << solve(input) << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin >> n;

	while(n--)
	{
		map<char,int> P;
		string str;
		string set;
		int mi=1026,mx=0;

		cin >> str;

		int count=1000;
		for(int i=0;i<str.length();i++)
		{
			P[str[i]]=count;
			for(int j=0;j<2;j++)set+=str[i+j];
			if(set=="<-")count--;
			else if(set=="->")count++;
			set.erase(set.begin(),set.end());

			mi=min(mi,count);
			mx=max(mx,count);
		}
		for(int i=mi;i<=mx;i++)
		{
			for(int j=0;j<26;j++)
			{
				char ans;
				ans = j + 'a';
				if(P[ans]==i)cout << ans ;
			}		
		}
		cout << endl;
	}
	
	return 0;
}