// Fresh Peach Heart Shower
#include <bits/stdc++.h>
#define pb push_back
#define MP std::make_pair
#define ALL(x) (x).begin(),(x).end()
#define mem(x,y) memset(x,y,sizeof(x))
#ifdef _LOCAL_
#define dprintf(x...) fprintf(stderr,x)
#else
#define dprintf(x...) 42
#endif
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,b,a) for(int i=(b);i>=(a);--i)
typedef std::vector<int > poly;
typedef std::pair<int,int > pii;
void setIn(std::string s){freopen(s.c_str(),"r",stdin);}
void setOut(std::string s){freopen(s.c_str(),"w",stdout);}
void setIO(std::string s){setIn(s+".in"),setOut(s+".out");}
template <class t> inline bool ckmin(t&x,t y){if(x>y)return x=y,1;return 0;}
template <class t> inline bool ckmax(t&x,t y){if(x<y)return x=y,1;return 0;}
inline int lowbit(int x){return x&(-x);}
std::deque<int > Q[305];
signed main(void)
{
#ifndef _LOCAL_
	std::ios::sync_with_stdio(false),std::cin.tie(0);
#endif

	std::ifstream data("meow.in");
	std::ifstream out("meow.out");
	
	int t;data>>t;
	while(t--)
	{
		int n,m,K;data>>n>>m>>K;
		rep(i,1,n)Q[i].clear();
		int q;out>>q;
		int op,x,y;
		rep(i,1,q)
		{
//			dprintf("i %d\n",i);
			out>>op;
			if(op==1)
			{
				out>>x;
				data>>y;
				if(Q[x].size()&&Q[x].back()==y)Q[x].pop_back();
				else Q[x].pb(y);
			}
			else
			{
				out>>x>>y;
				assert(Q[x].front()==Q[y].front());
				Q[x].pop_front(),Q[y].pop_front();
			}
		}
		rep(i,1,n)if(Q[i].size())assert(false);
	}

	return 0;
}


