#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
int n, m, k, N, t;
char ch[maxn];
int main()
{
scanf("%d", &t);
getchar();
while (t--) {
gets(ch);
n = strlen(ch);
int i;
m = 0;
for (i = n - 1; i >= 5; --i)
{
if (ch[i] == ' '&&ch[i - 1] == 't'&&ch[i - 2] == 'n'&&ch[i - 3] == 'i'&&ch[i - 4] == 'r'&&ch[i - 5] == 'p')
ch[i] = '(', m++;
}
for (int j = 0; j < n; ++j)
printf("%c", ch[j]);
for (i = 1; i <= m; ++i)
printf(")");
printf("\n");
}
}