/*
  [BAPC'06] DECOMPRESSION
  by: Jan Kuipers
*/

using namespace std;

#include <iostream>
#include <vector>
#include <string>
#include <cctype>

int main () {

  int runs;
  cin>>runs;
  while (runs--) {
    string s;
    cin>>s;

    int S=s.size();
    string t;
    char c;
    int idx=0, cnt;
    
    while (idx<S) {
      c=s[idx];
      cnt=0;
      idx++;
      while (idx<S && isdigit(s[idx])) {
	cnt = 10*cnt + s[idx]-'0';
	idx++;
      }
      if (cnt==0) cnt=1;
      t += string(cnt,c);
    }

    int N=t.size(),end=0;
    vector<int> lcnt(26,0), x(N), y(N);

    for (int i=0; i<N; i++) if (t[i]=='.') end=i, x[end]=0;
    for (int i=0; i<N; i++) if (i!=end) x[i]=lcnt[t[i]-'A']++;
    for (int i=1; i<26; i++) lcnt[i]+=lcnt[i-1];
    for (int i=0; i<N; i++) if (i!=end && t[i]>'A') x[i]+=lcnt[t[i]-'A'-1];
    for (int i=0; i<N; i++) if (i!=end) x[i]++;
    for (int i=0; i<N; i++) y[x[i]]=i;

    int now=y[end];

    for (int i=0; i<N; i++) {
      cout<<t[now];
      now=y[now];
    }
    cout<<endl;
  }

  return 0;
}
