#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>using namespace std;#define UD -1#define LR -2#define maxn 256#define maxlen 1024const double EPS = 1e-2;typedef struct node{ int w, h; int type; int lson, rson;}node;node win[maxn];char str[maxlen][maxlen];bool isletter(char x){ if (x!='-' && x!='|') return true; return false;}void streach_w(int k, int w){ win[k].w = w; if (win[k].type>=0) return; else if (win[k].type==UD) { streach_w(win[k].lson, w); streach_w(win[k].rson, w); } else { int w1 = (int)(ceil(1.0*w*win[win[k].lson].w/(win[win[k].lson].w+win[win[k].rson].w))+EPS); streach_w(win[k].lson, w1); streach_w(win[k].rson, w-w1); win[k].w=w; }}void streach_h(int k, int h){ win[k].h = h; if (win[k].type>=0) return; else if (win[k].type==LR) { streach_h(win[k].lson, h); streach_h(win[k].rson, h); } else { int h1 = (int)(ceil(1.0*h*win[win[k].lson].h/(win[win[k].lson].h+win[win[k].rson].h))+EPS); streach_h(win[k].lson, h1); streach_h(win[k].rson, h-h1); }}void print(int w, int h, char str[][maxlen]){ for (int i=0; i<=h; i++) { for (int j=0; j<=w; j++) printf("%c", str[i][j]); printf("n"); }}void clear(int h, int w, char str[][maxlen]){ for (int j=0; j<=w; j++) str[0][j] = str[h][j] = '-'; for (int i=0; i<=h; i++) str[i][0] = str[i][w] = '|'; str[0][0] = str[0][w] = str[h][0] = str[h][w] = '*'; for (int i=1; i<h; i++) for (int j=1; j<w; j++) str[i][j]=' ';}void cre_window(int k, int line1, int line2, int col1, int col2, char str[][maxlen]){ if (win[k].type>=0) { str[line1][col1] = win[k].type + 'A'; return; } else if (win[k].type==UD) { int line = line1 + win[win[k].lson].h; for (int i=col1+1; i<col2; i++) str[line][i] = '-'; str[line][col1] = str[line][col2] = '*'; cre_window(win[k].lson, line1, line, col1, col2, str); cre_window(win[k].rson, line, line2, col1, col2, str); } else { int col = col1 + win[win[k].lson].w; for (int i=line1+1; i<line2; i++) str[i][col] = '|'; str[line1][col] = str[line2][col] = '*'; cre_window(win[k].lson, line1, line2, col1, col, str); cre_window(win[k].rson, line1, line2, col, col2, str); }}int cal_size(int k, int l, int ∑, char tree[]){ if (isletter(tree[l])) { win[k].w = win[k].h = 2; win[k].lson = win[k].rson = -1; win[k].type = tree[l]-'A'; if (k==0) { clear(win[0].h, win[0].w, str); cre_window(0, 0, win[0].h, 0, win[0].w, str); print(win[0].w, win[0].h, str); } return l; } else{ int tmp; win[k].lson = ++sum; tmp = cal_size(win[k].lson, l+1, sum, tree); win[k].rson = ++sum; tmp = cal_size(win[k].rson, tmp+1, sum, tree); win[k].type = (tree[l]=='-') ? UD : LR; if (win[k].type==UD) { if (win[win[k].lson].w!=win[win[k].rson].w) { if (win[win[k].lson].w > win[win[k].rson].w) { win[k].w = win[win[k].lson].w; streach_w(win[k].rson, win[k].w); } else { win[k].w = win[win[k].rson].w; streach_w(win[k].lson, win[k].w); } } else win[k].w = win[win[k].lson].w; win[k].h = win[win[k].lson].h + win[win[k].rson].h; if (k==0) { clear(win[0].h, win[0].w, str); cre_window(0, 0, win[0].h, 0, win[0].w, str); print(win[0].w, win[0].h, str); } return tmp; } else { if (win[win[k].lson].h!=win[win[k].rson].h) { if (win[win[k].lson].h > win[win[k].rson].h) { win[k].h = win[win[k].lson].h; streach_h(win[k].rson, win[k].h); } else { win[k].h = win[win[k].rson].h; streach_h(win[k].lson, win[k].h); } } else win[k].h = win[win[k].lson].h; win[k].w = win[win[k].lson].w + win[win[k].rson].w; if (k==0) { clear(win[0].h, win[0].w, str); cre_window(0, 0, win[0].h, 0, win[0].w, str); print(win[0].w, win[0].h, str); } return tmp; } } return -1;}int main(){ int cs, sum; char tree[maxlen]; node win[maxn]; scanf("%d", &cs); for (int cases=1; cases<=cs; cases++) { sum=0; scanf("%s", tree); printf("%dn", cases); cal_size(0, 0, sum, tree); } return 0;}