#include<iostream>
#include<vector>
#include<algorithm>
#include<cassert>
#include<deque>
#include<sstream>
#include<map>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(_,n) REP(_,0,n)
#define F first
#define S second
using namespace std;
typedef pair<string,int> si;
vector<vector<char> > op;
vector<char> dir;
string line;
int size;
int T,G,N;
vector<map<char,bool> > vmcb;

string cnuf(vector<string> &vec)
{
  //cout << "-----------cnuf--------------" << endl;
  //rep(i,vec.size())cout << "vec : " << vec[i] << endl;
  int n = vec.size();
  for(int i=G-1;i>=0;i--)
    {
      if(dir[i] == 'L')
	{
	  for(int j=1;j<n;j+=2)
	    {
	      //cerr << "opera = "  << vec[j] << endl;
	      assert(vec[j].size() == 1);
	      if(vmcb[i][vec[j][0]])
		{
		  vector<string> vesc;
		  string compo = "(";
		  rep(k,n)
		    {
		      if(k+1 == j || k-1 == j || k == j)
			{
			  if(k-1 == j)
			    {
			      compo += vec[k]+")";
			      vesc.push_back(compo);
			      continue;
			    }
			  else if(k+1 == j)
			    {
			      compo += vec[k];
			      continue;
			    }
			  else
			    {
			      compo += vec[k];
			      continue;
			    }
			}
		      else
			vesc.push_back(vec[k]);
		    }
		  return cnuf(vesc);
		}
	    }
	}
      else if(dir[i] == 'R')
	{	  
	  int st = vec.size()-1;
	  if(st%2 == 0)st--;
	   
	  for(int j=st;j>=0;j-=2)
	    {
	      assert(vec[j].size() == 1);
	      if(vmcb[i][vec[j][0]])
		{
		  vector<string> vesc;
		  string compo = "(";
		  rep(k,n)
		    {
		      if(k+1 == j || k-1 == j || k == j)
			{
			  if(k-1 == j)
			    {
			      compo += vec[k]+")";
			      vesc.push_back(compo);
			      continue;
			    }
			  else if(k+1 == j)
			    {
			      compo += vec[k];
			      continue;
			    }
			  else
			    {
			      compo += vec[k];
			      continue;
			    }
			}
		      else
			vesc.push_back(vec[k]);
		    }
		  return cnuf(vesc);
		}
	    }
	}


    }
  string ret;
  rep(i,vec.size())ret += vec[i];
  return ret;
}

si func(int p)
{
  vector<string> vec;
  //cout << "fuc : "<< line.substr(p,line.size()-p) << endl;
  int j=p;
  REP(i,p,size)
    {
      if(line[i] == ')')
	{
	  if(i-p == 3)
	    {
	      //cout << "end func ret : " << "(" << line.substr(p,3) << ")" << endl;
	      return si("("+line.substr(p,3)+")",i);	
	    }
	  break;
	}
      else if(line[i] == '(')
	{
	  si SI = func(i+1);
	  vec.push_back("("+SI.F+")");
	  i = SI.S;
	  j = i+1;
	  //cout << "p = " << p << " i = " << i << endl;
	  continue;
	}
      j++;
      vec.push_back(line.substr(i,1));
    }
  //cout << "pre end;" << endl;
  //rep(i,vec.size())cout << vec[i] << endl;
  //cout << "end func : " << endl;
  return si(cnuf(vec),j);
}

string Remove(string s)
{
  //cout << "Remove : " << s << endl;
  rep(i,s.size())
    {
      //cout << "i = " << i << " : " << s << endl;
      if(s[i] != '(')continue;

      int cnt = 0;
      bool ok = false;
      REP(j,i,s.size())
	{
	  if(s[j] == '(')cnt++;
	  else if(s[j] == ')')cnt--;
	  if(cnt == 0)
	    {
	      //cout << s[i] << " " << s[j] << endl; 
	      if(!ok)
		{
		  s[i] = ' ';
		  s[j] = ' ';
		}
	      break;
	    }
	  if(cnt == 1 && (s[j] == '+' || s[j] == '-' || s[j] == '*' || s[j] == '/' || s[j] == '<' || s[j] == '>' || s[j] == '=' || s[j] == '&' || s[j] == '|' || s[j] == '^'))ok = true;
	}
    }
  stringstream ss;
  ss << s;
  string ret;
  while(!(ss >> s).fail())ret += s;
  return ret;
}

int main()
{

  cin >> T;
  bool f = false;
  while(T-- > 0)
    {
      if(f)cout << endl;
      if(!f)f = true;
      cin >> G;
      op.clear();
      dir.clear();
      vmcb.clear();
      op.resize(G);
      dir.resize(G);
      vmcb.resize(G);

      rep(i,G)
	{
	  cin >> dir[i];
	  int n;
	  cin >> n;
	  rep(j,n)
	    {
	      char opera;
	      cin >> opera;
	      op[i].push_back(opera);
	      vmcb[i][opera] = true; 
	    }
	}


      vector<string> vec;
      cin >> N;
      cin.ignore();
      rep(_,N)
	{
	 
	  getline(cin,line);
	  //cout << "LINE : " << line << endl;
	  line = "(" + line + ")";
	  size = line.size();
	  cout << Remove(func(1).F) << endl;
	}
    }
  return 0;
}

Measured memory_bytes: 1376