#pragma warning(disable:4996)
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<set>
#include<map>
#include<string>
#include<stack>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<stdlib.h>
using namespace std;
const int MAX_N = 680;
#define INF 0x3f3f3f3f
#define LL long long
typedef pair<int, int> PII;
#define MP make_pair
#define pb push_back
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FOR0(i,n) for(int i=0;i<n;i++)
#define FORL0(i,n) for(int i=n;i>=0;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define lson l,m,,rt<<1
#define rson m+1,r,rt<<1|1
int n, m, k, t, ans, cas, x, y, num,s,a,b,c;
void RD(int &x){ scanf("%d", &x); }
void RD(int &x, int &y){ scanf("%d%d", &x, &y); }
void RD(double &x){ scanf("%lf", &x); };
void RD(char &x){ scanf("%c", &x); }
void PR(int x){ printf("%d\n", x); }
void PR(LL x){ printf("%lld\n", x); }
void PRK(int x) { printf("%d ", x); }
void PL(){ puts(""); }
//void PR()
//int a[100005];
int sum[10];
bool ok(int x){
for (int i = 0; i + x - 1 < n; i++){
int j = i + x - 1;
if (sum[j + 1] - sum[i] >= s) return true;
}
return false;
}
int main()
{
//freopen("aaa.txt", "r", stdin);
//freopen("bbb.txt","w",stdout);
cin >> x;
cas = 1;
//sum[0] = 0;
while (x--){
printf("Case #%d:\n",cas++);
map<int, map<int, int> > grid;
map<int, map<int, int> >::iterator gite;
map<int, int> colj;
map<int, int>::iterator cite;
map<int, int> rowc;
map<int, int> colc;
cin >> n >> m >> k;
FOR0(i, k){
scanf("%d%d%d", &a, &b, &c);
grid[a].insert(make_pair(b,c));
if (rowc.find(a) == rowc.end()) rowc[a] =a;
if (colc.find(b) == colc.end()) colc[b] = b;
colj[b] = 1;
}
cin >> t;
FOR0(i, t) {
scanf("%d%d%d",&a,&b,&c);
if (a == 1 ){
if (rowc.find(b) == rowc.end()) break;
if (rowc.find(c) == rowc.end()) break;
swap(rowc[b],rowc[c]);
}
else if (a == 2){
if (colc.find(b) == colc.end()) break;
if (colc.find(c) == colc.end()) break;
swap(colc[b],colc[c]);
}
else {
b = rowc[b];
c = colc[c];
if (grid[b][c] != 0) printf("%d\n", grid[b][c]);
else puts("0");
/*gite = grid.find(b);
if (gite == grid.end()) puts("0");
else{
cite = gite->second.find(c);
if (cite != gite->second.end()) {
int tmp = cite->second;
printf("%d\n",tmp);
}
else
}*/
}
}
}
//while (1);
return 0;
}