// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#define dibs reserve
#define OVER9000 1234567890
#define tisic 47
#define soclose 10e-7
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define uint unsigned int
// mylittlepony
using namespace std;
int main() {
int T;
cin >> T;
for(int i =0; i < T; i++) {
int N;
cin >> N;
map<string,int> row;
map<string,int> col;
map< pair<string,string>, string> M;
for(int i =0; i < N; i++) {
string s,t,u;
cin >> s >> t >> u;
M[make_pair(s,t)] =u;
row[s] =0;
col[t] =0;}
int a =0;
ALL_THE(row,it) {it->ss =a; a++;}
a =0;
ALL_THE(col,it) {it->ss =a; a++;}
vector<string> V(3+2*row.size());
for(int i =0; i < V.size(); i++) {
for(int j =0; j < 5+4*col.size(); j++) {
if(i%2 == 0) V[i] +="-";
else V[i] +=" ";}
if(i%2 == 0) for(int j =0; j < V[i].length(); j +=4) V[i][j] ='+';
else for(int j =0; j < V[i].length(); j +=4) V[i][j] ='|';}
ALL_THE(M,it) {
int x =row[it->ff.ff], y =col[it->ff.ss];
for(int j =0; j < (it->ss).length(); j++) V[2*x+3][4*y+5+j] =(it->ss)[j];
}
ALL_THE(row,it) for(int j =0; j < (it->ff).length(); j++)
V[2*(it->ss)+3][1+j] =(it->ff)[j];
ALL_THE(col,it) for(int j =0; j < (it->ff).length(); j++)
V[1][4*(it->ss)+5+j] =(it->ff)[j];
for(int i =0; i < V.size(); i++) cout << V[i] << "\n";}
return 0;}
// look at my code
// my code is amazing
Ly8gaW9zdHJlYW0gaXMgdG9vIG1haW5zdHJlYW0KI2luY2x1ZGUgPGNzdGRpbz4KLy8gYml0Y2ggcGxlYXNlCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPG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cjxzdHJpbmc+IFYoMysyKnJvdy5zaXplKCkpOwogICAgCWZvcihpbnQgaSA9MDsgaSA8IFYuc2l6ZSgpOyBpKyspIHsKICAgIAkJZm9yKGludCBqID0wOyBqIDwgNSs0KmNvbC5zaXplKCk7IGorKykgewogICAgCQkJaWYoaSUyID09IDApIFZbaV0gKz0iLSI7CiAgICAJCQllbHNlIFZbaV0gKz0iICI7fQogICAgCQlpZihpJTIgPT0gMCkgZm9yKGludCBqID0wOyBqIDwgVltpXS5sZW5ndGgoKTsgaiArPTQpIFZbaV1bal0gPScrJzsKICAgIAkJZWxzZSBmb3IoaW50IGogPTA7IGogPCBWW2ldLmxlbmd0aCgpOyBqICs9NCkgVltpXVtqXSA9J3wnO30KICAgIAlBTExfVEhFKE0saXQpIHsKICAgIAkJaW50IHggPXJvd1tpdC0+ZmYuZmZdLCB5ID1jb2xbaXQtPmZmLnNzXTsKICAgIAkJZm9yKGludCBqID0wOyBqIDwgKGl0LT5zcykubGVuZ3RoKCk7IGorKykgVlsyKngrM11bNCp5KzUral0gPShpdC0+c3MpW2pdOwogICAgCQl9CiAgICAJQUxMX1RIRShyb3csaXQpIGZvcihpbnQgaiA9MDsgaiA8IChpdC0+ZmYpLmxlbmd0aCgpOyBqKyspIAogICAgCQlWWzIqKGl0LT5zcykrM11bMStqXSA9KGl0LT5mZilbal07CiAgICAJQUxMX1RIRShjb2wsaXQpIGZvcihpbnQgaiA9MDsgaiA8IChpdC0+ZmYpLmxlbmd0aCgpOyBqKyspIAogICAgCQlWWzFdWzQqKGl0LT5zcykrNStqXSA9KGl0LT5mZilbal07CiAgICAJZm9yKGludCBpID0wOyBpIDwgVi5zaXplKCk7IGkrKykgY291dCA8PCBWW2ldIDw8ICJcbiI7fQogICAgcmV0dXJuIDA7fQogICAgICAgIAovLyBsb29rIGF0IG15IGNvZGUKLy8gbXkgY29kZSBpcyBhbWF6aW5n