import java.io.*;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.Vector;
class picture{
char[][] pic;
int h;
int w;
String name;
picture(int h,int w,TreeMap<String,picture> picTM,Vector<css> cssV){
this.h=h;
this.w=w;
pic = new char[h][w];
int[][] lay = new int[h][w];
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
lay[i][j]=0;
pic[i][j]='.';
}
}
for(int i=0;i<cssV.size();i++){
css c =cssV.elementAt(i);
picture p = picTM.get(c.getPicFile());
if(p!=null){
char[][] pc =p.getCharArray();
int startH=c.getTopLeftX(),startW=c.getTopLeftY(),H=p.getH(),W=p.getW(),layer=c.getLayer();
for(int j=startH;j-startH<H&&j<h;j++){
for(int k=startW;k-startW<W&&k<w;k++){
char ch=pc[j-startH][k-startW];
if(j>=0&&k>=0){
if(ch!='.'){//||ch!=' '){
if(layer>=lay[j][k]){
pic[j][k]=ch;
lay[j][k]=layer;
}else{
if(pic[j][k]=='.'){//&&pic[j][k]==' '){
pic[j][k]=ch;
lay[j][k]=layer;
}
}
}
}
}
}
}
}
}
picture(String name,int h,int w,Scanner cin){// throws Exception{
//BufferedReader br = new BufferedReader(isr);
this.h=h;
this.w=w;
this.name=name;
pic = new char[h][w];
for(int i=0;i<h;i++){
pic[i]=cin.next().toCharArray();
}
}
public void display(){
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(pic[i][j]!='.')
System.out.print(pic[i][j]);
else
System.out.print(" ");
}
System.out.println();
}
}
public int getH(){
return this.h;
}
public int getW(){
return this.w;
}
public char[][] getCharArray(){
return this.pic;
}
}
class css{
int posX;
int posY;
int layer;
String name;
String picFile;
boolean posAbsolute;
String cssFile;
int topLeftX;
int topLeftY;
int maxX;
int maxY;
css(Scanner cin){//throws Exception{
// InputStreamReader irs = new InputStreamReader(System.in);
//FileOutputStream fos = new FileOutputStream("D:\\css.txt");
//DataOutputStream dos = new DataOutputStream(fos);
int times=0;
String str="";
String newLine;
char ch=' ';
// System.out.println("*");
while(times<7){
newLine=cin.nextLine();
// System.out.println(newLine+":"+times);
for(int i=0;i<newLine.length();i++){
ch =newLine.charAt(i);
//dos.writeChar(chi);
// System.out.println(chi+":"+ch);
switch(ch){
case '#':break;
case '{':name=str;str="";break;
case '}':break;
case '\n':break;
case '\r':break;
case '\t':break;
case ' ':break;
case ':':str="";break;
case ';':
// System.out.println(str+":"+times);
switch(times){
case 0:break;
case 1:
if(str.length()>2){
str=str.substring(0,str.length()-2);
if(isNumber(str))
posX=Integer.parseInt(str);
}
break;
case 2:
if(str.length()>2){
str=str.substring(0,str.length()-2);
if(isNumber(str))
posY=Integer.parseInt(str);
}
break;
case 3:
if(str.equals("absolute")){
posAbsolute=true;
}
else{
posAbsolute=false;
int beginIndex=0;
while(beginIndex<str.length()&&str.charAt(beginIndex)!='=')beginIndex++;
beginIndex++;
if(beginIndex<str.length())
cssFile=str.substring(beginIndex);
}
break;
case 4:picFile=str;break;
case 5:if(isNumber(str))layer=Integer.parseInt(str);break;
default:
}
str="";
break;
default:str+=ch;
}
}
times++;
}
//dos.close();
}
boolean isNumber(String str){
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(i==0){
if((ch<'0'||ch>'9')&&ch!='-')
return false;
}
else{
if(ch<'0'||ch>'9')
return false;
}
}
return true;
}
public boolean isAbsolute(){
return this.posAbsolute;
}
public int getTopLeftX(){
return topLeftX;
}
public int getTopLeftY(){
return topLeftY;
}
public String getName(){
return this.name ;
}
public String getPicFile(){
return this.picFile;
}
public String getCssFile(){
return this.cssFile;
}
public int getLayer(){
return this.layer;
}
public boolean calculateTopLeftXY(TreeMap<String,css> cssTM){
if(posAbsolute){
topLeftX=posY;
topLeftY=posX;
}
else{
if(cssFile!=null){
css RelativeCss = cssTM.get(cssFile);
if(RelativeCss.posAbsolute){
topLeftX=posY+RelativeCss.getTopLeftX();
topLeftY=posX+RelativeCss.getTopLeftY();
posAbsolute=true;
}
}
}
return posAbsolute;
}
}
public class Main {
public static int test;
//public static int maxX;
//public static int maxY;
public static picture finalPic;
public static void run(Scanner cin){// throws Exception{
//Scanner cin = new Scanner(System.in);
//InputStreamReader isr = new InputStreamReader(System.in);
int picN,cssN,maxX=0,maxY=0;
picN=cin.nextInt();
TreeMap<String,picture> picTM = new TreeMap<String,picture>();
for(int i=0;i<picN;i++){
String name= cin.next();
int h = cin.nextInt();
int w = cin.nextInt();
picture pic = new picture(name,h,w,cin);
picTM.put(name, pic);
}
cssN = cin.nextInt();
TreeMap<String,css> cssTM = new TreeMap<String,css>();
Vector<css> cssV = new Vector<css>();
Vector<css> cssA = new Vector<css>();
Vector<css> cssR = new Vector<css>();
cin.nextLine();
for(int i=0;i<cssN;i++){
css cssFile = new css(cin);
cssTM.put(cssFile.getName(), cssFile);
cssV.add(cssFile);
if(cssFile.isAbsolute())
cssA.add(cssFile);
else
cssR.add(cssFile);
}
for(int i=0;i<cssA.size();i++){
css c =cssA.elementAt(i);
c.calculateTopLeftXY(cssTM);
picture pic = picTM.get(c.getPicFile());
if(maxX<pic.getH()+c.getTopLeftX())
maxX=pic.getH()+c.getTopLeftX();
if(maxY<pic.getW()+c.getTopLeftY())
maxY=pic.getW()+c.getTopLeftY();
}
for(int i=0;i<cssR.size();i++){
css c =cssR.elementAt(i);
c.calculateTopLeftXY(cssTM);
picture pic = picTM.get(c.getPicFile());
if(maxX<pic.getH()+c.getTopLeftX())
maxX=pic.getH()+c.getTopLeftX();
if(maxY<pic.getW()+c.getTopLeftY())
maxY=pic.getW()+c.getTopLeftY();
}
for(int i=0;i<cssV.size();i++){
css c = cssV.elementAt(i);
// c.calculateTopLeftXY(cssTM);
//System.out.println(c.getName()+":"+c.getTopLeftX()+" "+c.getTopLeftY()+" "+c.getCssFile()+" "+c.getPicFile());
}
finalPic = new picture(maxX>1000?1000:maxX,maxY>1000?1000:maxY,picTM,cssV);
//finalPic.display();
}
public static void main(String[] args){ Scanner cin = new Scanner(System.in);
int test = cin.nextInt();
for(int i=0;i<test;i++){
//run(cin,isr);
run(cin);
System.out.println("Scenario #"+(i+1)+":");
finalPic.display();
System.out.println();
}
}
}