// 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