#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
using namespace std;

struct Bnode{
	Bnode *l, *r;
	Bnode(){ l = r = NULL; }
	~Bnode(){ delete l; delete r; }
};

Bnode *root = NULL;

#define MAXL 11000
char orig[MAXL], rec[MAXL];
int ptr;

void compress(Bnode *x){
	if(x == NULL)
		return;
	rec[ptr++] = '(';
	compress(x->l);
	compress(x->r);
	rec[ptr++] = ')';
}

int dr[4] = {0, 1, 0, -1};
int dc[4] = {1, 0, -1, 0};

vector<vector<char> > G;
vector<vector<bool> > V;

#define BFMT 0
#define TMGP 1
#define DIFF 2

void nassert(bool cond, int code){
	if(!cond){
		if(code == BFMT)
			printf("Bad format\n");
		else if(code == TMGP)
			printf("Too many grid points\n");
		else if(code == DIFF)
			printf("Different tree\n");
		else
			printf("Wrong answer\n");

		delete root;
		exit(20);
	}
}

char edge(int &r, int &c){
	V[r][c] = true;
	if(G[r][c] == '|'){
		if(!V[r - 1][c])
			--r;
		else{
			nassert(!V[r + 1][c], BFMT);
			++r;
		}
	}else{ // G[r][c] == '-'
		if(!V[r][c - 1])
			--c;
		else{
			nassert(!V[r][c + 1], BFMT);
			++c;
		}
	}

	for(;;){
		V[r][c] = true;
		if(G[r][c] == 'L' || G[r][c] == 'R')
			return G[r][c];
		nassert(G[r][c] == '+', BFMT);

		int vis = 0;
		int w = -1;
		REP(d, 4){
			int nr = r + dr[d];
			int nc = c + dc[d];
			if(G[nr][nc] != '.'){
				if(V[nr][nc])
					++vis;
				else{
					nassert(w == -1, BFMT);
					w = d;
					V[nr][nc] = true;
				}
			}
		}
		nassert(vis == 1, BFMT);
		nassert(w != -1, BFMT);

		r += 2 * dr[w];
		c += 2 * dc[w];
		nassert(!V[r][c], BFMT);
		nassert(V[r][c] != '.', BFMT);
	}
}

Bnode *DFS(int r, int c){
	Bnode *ret = new Bnode;

	REP(d, 4){
		int nr = r + dr[d];
		int nc = c + dc[d];
		if(G[nr][nc] != '.' && !V[nr][nc]){
			if(edge(nr, nc) == 'L'){
				nassert(ret->l == NULL, BFMT);
				ret->l = DFS(nr, nc);
			}else{
				nassert(ret->r == NULL, BFMT);
				ret->r = DFS(nr, nc);
			}
		}
	}

	nassert(ret->l != NULL || ret->r == NULL, BFMT);

	return ret;
}

// stdin <- contestant's output
// argv[1] <- input
int main(int argc, char** argv){
	FILE *input = fopen(argv[1], "r");
	int T;
	fscanf(input, "%d ", &T);

	while(T--){
		int K;
		fscanf(input, "%d ", &K);
		fgets(orig, MAXL, input);

		int R, C;
		nassert(scanf("%d %d ", &R, &C) == 2, BFMT);
		nassert(1 <= R && 1 <= C, BFMT);
		nassert((long long)R * (long long)C <= K, TMGP);

		G.clear();
		G.resize(2 * R + 3, vector<char>(2 * C + 3, '.'));
		FOR(i, 2, 2 * R) FOR(j, 2, 2 * C)
			nassert(scanf("%c ", &G[i][j]) == 1, BFMT);

		int hr = -1, hc = -1;
		FOR(i, 2, 2 * R) FOR(j, 2, 2 * C)
			if(i % 2 == 0){
				if(j % 2 == 0){
					nassert(G[i][j] == '.' || G[i][j] == '+' ||
					       G[i][j] == 'L' || G[i][j] == 'R' || G[i][j] == 'H', BFMT);
					if(G[i][j] == 'H'){
						nassert(hr == -1, BFMT);
						hr = i;
						hc = j;
					}
				}else
					nassert(G[i][j] == '.' || G[i][j] == '-', BFMT);
			}else{
				if(j % 2 == 0)
					nassert(G[i][j] == '.' || G[i][j] == '|', BFMT);
				else
					nassert(G[i][j] == '.', BFMT);
			}
		nassert(hr != -1, BFMT);

		V.clear();
		V.resize(2 * R + 3, vector<bool>(2 * C + 3, false));
		V[hr][hc] = true;
		root = DFS(hr, hc);

		FOR(i, 2, 2 * R) FOR(j, 2, 2 * C)
			if(G[i][j] != '.')
				nassert(V[i][j], BFMT);

		ptr = 0;
		compress(root);
		rec[ptr++] = '\n';
		rec[ptr++] = '\0';

		nassert(strcmp(orig, rec) == 0, DIFF);
	}

	nassert(feof(stdin) != 0, BFMT);
	fclose(input);
	delete root;
	return 0;
}
