/*
DECOMPRESSION Hendrik Jan Hoogeboom
- inlezen en expanderen (dwz cijfers wegwerken)
- sorteervolgorde regels bepalen (door letters te tellen)
- decoderen door sorteervolgorde terug te volgen 
*/
using namespace std;

#include <iostream>
#include <string>

int main ()
{
//cout << ". " << (short) '.' << endl;
//cout << "A " << (short) 'A' << endl;
//cout << "Z " << (short) 'Z' << endl;
  int Ninst, len, num, i;
  // string class iterator
  string::const_iterator p;
  string S;
  char c;
// om te expanderen:
  string expand;
  int newlen;
// om te sorteren:
  int tellen[128];
// om te decoderen:
  int invers[1000000], startpos ;

  cin >> Ninst;
  while ( Ninst-- )
  {
    expand = "";
    cin >> S;
    newlen = 0;
    len = S.length();
//    cout << S << " : " << len << " " << S[0] << " " ;
    p=S.begin();
    while ( p!= S.end() )
    {
      c=*p; p++;
      // followed by number?
      num = 0;
      while ( p!=S.end() && isdigit(*p) )
      { num *= 10; num += *p-'0'; p++; }
      newlen += num ? num : 1;
      if (num) { expand.append(num, c); } 
      else  { expand += c; }
    }
//    if (newlen - expand.length()) cerr << " lengte !?!" ;

    // sorting by counting
    for (i=0 ; i<= 127 ; i++) { tellen[i] = 0; }
    p=expand.begin();
    while ( p!= expand.end() )
    {
      tellen[ (short)*p ]++;
      p++;
    }

    for (i= (short)'A' ; i<= (short)'Z' ; i++) tellen[i] += tellen[i-1];
    for (i= (short)'Z' ; i> (short)'A' ; i--) tellen[i] = tellen[i-1];
    tellen[(short)'A'] = 0;
    // tellen[i] bevat nu steeds het aantal letters alfabetisch voor letter 'i'

    p=expand.begin();
    i = 0;
    while ( p!= expand.end() )
    {
      if (*p=='.') {
//      sorteervolgorde[i] = 0;
        startpos = i;
      }
      else {
        tellen[ (short)*p ]++ ;
//      sorteervolgorde[i] = tellen[ (short)*p ] + eerder[ (short)*p ];
        invers[ tellen[ (short)*p ] ] = i;
      }
      p++; i++; 
    }
/*
    cerr << "sorted : " ;
    for (i=0; i< expand.length(); i++) cerr << expand[i];
    cerr << endl;
*/   
    num = startpos;
    for (i=1; i< expand.length(); i++) 
    { num = invers[num]; cout << expand[num]; }
    cout << expand[startpos] << endl;

  }
}
