#include <bits/stdc++.h>
typedef unsigned u32;
const int N = 16, M = 12, _2NM = 65536;
int n, m, type;
u32 a[N - M], g[N], h[N - M];
u32 synd[_2NM], cole[_2NM];
int wcur[_2NM], numsynd;
u32 toenc, todec, syndrome;
void mm(u32 *x, u32 *ty, u32 *z, int la, int lb, int lc) {
for (int i = 0; i < la; ++ i) {
z[i] = 0;
for (int j = 0; j < lc; ++ j) {
z[i] |= (1u << __builtin_parity(x[i] & ty[j]));
}
}
return;
}
void mv(u32 *x, u32 y, u32 &z, int la, int lb) {
z = 0;
for (int i = 0; i < la; ++ i) {
z |= (u32(__builtin_parity(x[i] & y)) << i);
}
return;
}
void get_coset_leader(void) {
numsynd = 0;
for (u32 i = 1; i < (1u << n); ++ i) {
u32 now = 0, temp = 0;
int w = 0;
for (int j = 0; j < n; ++ j) {
now |= (((i >> j) & 1) << (n - 1 - j));
w += ((i >> j) & 1);
}
mv(h, now, temp, n - m, n);
int ind = -1;
for (int j = 0; j < numsynd; ++ j) {
if (temp == synd[j]) {
ind = j;
break;
}
}
if (ind == -1) {
++ numsynd;
synd[numsynd - 1] = temp;
cole[numsynd - 1] = now;
wcur[numsynd - 1] = w;
} else if (w < wcur[ind]) {
wcur[ind] = w;
cole[ind] = now;
}
}
return;
}
void encode(void) {
todec = 0;
mv(g, toenc, todec, n, m);
return;
}
void decode(void) {
syndrome = 0;
mv(h, todec, syndrome, n - m, n);
if (syndrome == 0) {
return;
}
int ind = -1;
for (int i = 0; i < numsynd; ++ i) {
if (syndrome == synd[i]) {
ind = i;
break;
}
}
if (ind == -1) {
puts("There is something wrong!");
exit(-1);
} else {
todec xor_eq cole[ind];
}
return;
}
int main(void) {
int T;
scanf("%d", &T);
while (T --) {
scanf("%d%d%d", &n, &m, &type);
for (int i = 0; i < n - m; ++ i) {
a[i] = h[i] = 0;
}
for (int i = 0; i < n; ++ i) {
g[i] = 0;
}
for (int i = 0, temp; i < n - m; ++ i) {
for (int j = 0; j < m; ++ j) {
scanf("%d", &temp);
a[i] |= (u32(temp) << j);
}
}
for (int i = 0; i < m; ++ i) {
g[i] = (1u << i);
}
for (int i = 0; i < n - m; ++ i) {
g[i + m] = a[i];
h[i] = a[i];
}
for (int i = 0; i < n - m; ++ i) {
h[i] |= (1u << (i + m));
}
if (type == 0) {
numsynd = (1 << (n - m));
for (int i = 0, temp; i < (1 << (n - m)); ++ i) {
synd[i] = cole[i] = 0;
for (int j = 0; j < n - m; ++ j) {
scanf("%d", &temp);
synd[i] |= (u32(temp) << j);
}
for (int j = 0; j < n; ++ j) {
scanf("%d", &temp);
cole[i] |= (u32(temp) << j);
}
}
} else {
get_coset_leader();
}
int q, inpo;
scanf("%d", &q);
while (q --) {
scanf("%d", &inpo);
if (inpo == 0) {
toenc = 0;
for (int i = 0, temp; i < m; ++ i) {
scanf("%d", &temp);
toenc |= (u32(temp) << i);
}
encode();
} else if (inpo == 1) {
todec = 0;
for (int i = 0, temp; i < n; ++ i) {
scanf("%d", &temp);
todec |= (u32(temp) << i);
}
decode();
}
for (int i = 0; i < n; ++ i) {
printf("%d ", int((todec >> i) & 1));
}
puts("");
}
}
return 0;
}