/* Robin Nittka */
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cctype>
#include <cstring>

using namespace std;

vector<vector<char> > levels;
int maxi;

void dfs(const char * &s, int dep)
{
	maxi >?= dep;
	levels[dep].push_back(*s);
	if (isupper(*s))
	{
		s++;
		dfs(s, dep+1);
		dfs(s, dep+1);
	}
	else
		s++;
}

int main()
{
	int n;
	for (cin >> n; n; n--)
	{
		char in[10010];
		cin >> in;
		int len = strlen(in);

		levels.assign(10010, vector<char>());
		reverse(in, in+len);
		maxi = 0;
		const char *ptr = in;
		dfs(ptr, 0);

		char res[10010];
		int pos = 0;
		for (int i = maxi; i >= 0; i--)
			for (vector<char>::const_iterator it = levels[i].begin(); it != levels[i].end(); ++it)
				res[pos++] = *it;
		res[pos] = 0;
		puts(res);
	}
	return 0;
}
