#include<bits/stdc++.h>
using namespace std;
constexpr int N=300,M=1000000;
int a[M+10];
deque<int> q[N+10];
int main(){
	ifstream fin,fout;
	fin.open("meow.in");
	fout.open("meow.out");
	int T;
	fin>>T;
	int ii;
	for(ii=1;ii<=T;++ii){
		int n,m,K;
		fin>>n>>m>>K;
		int i;
		for(i=1;i<=m;++i) fin>>a[i];
		int cnt;
		fout>>cnt;
		int top=0;
		for(i=1;i<=cnt;++i){
			int op;
			fout>>op;
			if(op==1){
				int x;
				fout>>x;
				++top;
				if(top>m){
					cout<<"TestCase #"<<ii<<",oper #"<<i<<":top>m\n";
					return 1;
				}
				if(!q[x].empty()&&q[x].front()==a[top]) q[x].pop_front();
				else q[x].push_front(a[top]);
			}
			else{
				int u,v;
				fout>>u>>v;
				if(u==v){
					cout<<"TestCase #"<<ii<<",oper #"<<i<<":u==v\n";
					return 1;
				}
				if(q[u].empty()){
					cout<<"TestCase #"<<ii<<",oper #"<<i<<":stack["<<u<<"] is empty\n";
					return 1;
				}
				if(q[v].empty()){
					cout<<"TestCase #"<<ii<<",oper #"<<i<<":stack["<<v<<"] is empty\n";
					return 1;
				}
				if(q[u].back()!=q[v].back()){
					cout<<"TestCase #"<<ii<<",oper #"<<i<<":bottom of stack["<<u<<","<<v<<"] not equal\n";
					return 1;
				}
				q[u].pop_back(),q[v].pop_back();
			}
		}
		if(top<m){
			cout<<"TestCase #"<<ii<<",top<m\n";
			return 1;
		}
		for(i=1;i<=n;++i){
			if(!q[i].empty()){
				cout<<"TestCase #"<<ii<<",stack["<<i<<"] is not empty\n";
				return 1;
			}
		}
	}
	return 0;
}
