#include <iostream>
#include <string>
using namespace std;

string helper(string& s, int& index) {
    string res, tmp;
    int x = 0;
    while(index < s.size()) {
        if(s[index] == '('){ #遇到左括号递归开始
            index++;
            res += helper(s,index);
        }else if(s[index] == ')') { #遇到右括号计算后面的数字,算完之后return
            index++;
            while(s[index] >= '0' && s[index] <= '9') {
                x = x*10+(s[index++]-'0');
            }
            tmp = res;
            while(--x)
                res += tmp;
            return res;
        }else if(s[index] >= '0' && s[index] <= '9') {#直接遇到数字说明是单个字母的重复
            x = 0;
            while(s[index] >= '0' && s[index] <= '9') {
                x = x*10+(s[index++]-'0');
            }
            while(--x)
                res += res.back();
        }
        else{#其它字母直接加
            res += s[index++];
        }
    }
    return res;
}

int main() {
    int n = 0,index = 0;
    string s;
    getline(cin,s);
    n = stoi(s);
    for(int i = 0; i < n; i++) {
        getline(cin,s);
        index = 0;
        string res = helper(s,index);
        cout << res <<endl;
    }
    return 0;
}

来个C++版的