#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\n"
#ifndef ONLINE_JUDGE
#include "bits/debug.h"
#endif
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
/* ============================ CONFIG BEGIN ============================ */
constexpr int N = 2005;
constexpr int Q = 1e6 + 5;
bitset<N> G[N], H[N], vis[N];
int t[N];
vector<array<int, 3>> qlist[N];
vector<array<int, 2>> elist[N];
bitset<N> inq[N];
int ans[Q];
int buc[N];
int n, l, q;
int fa[N];
int root(int u) {
return u == fa[u] ? u : fa[u] = root(fa[u]);
}
queue<pair<int, int>> Queue;
int input() {
char a, b;
cin >> a >> b;
return (a - 48) * 50 + (b - 48);
}
void pushqueue(int u, int v) {
if (!inq[u].test(v)) {
Queue.push({u, v});
inq[u].set(v);
}
}
void work(int u, int v) {
G[u].set(v);
auto newE = G[u];
G[u] |= G[v];
newE ^= G[u];
while (1) {
int w = newE._Find_first();
if (w < 1 or w > n) break;
pushqueue(u, w);
newE.flip(w);
}
newE = H[v];
H[v] |= H[u];
newE ^= H[v];
while (true) {
int w = newE._Find_first();
if (w < 1 || w > n) {
break;
}
pushqueue(w, v);
newE.flip(w);
}
}
void add(int u, int v) {
if (G[u].test(v)) return;
pushqueue(u, v);
while (!Queue.empty()) {
auto [x, y] = Queue.front();
Queue.pop();
work(x, y);
}
}
/* ============================= CONFIG END ============================= */
void solve() {
cin >> n >> l >> q;
// iota(fa, fa + n + 1, 0);
FOR(id, 1, l) {
unordered_set<int> s;
fill(buc, buc + n + 1, 0);
FOR(i, 1, n) t[i] = input(), buc[t[i]]++;
int cnt1 = count_if(buc + 1, buc + n + 1, [&](auto a) { return a != 0; });
if (cnt1 <= n - 2) continue;
else if (cnt1 == n - 1) {
int m2 = 0, m0 = 0;
FOR(i, 1, n) {
if (buc[i] == 2) m2 = i;
if (buc[i] == 0) m0 = i;
}
FOR(i, 1, n) {
if (t[i] == m2) {
WARN(i, m0);
elist[id].push_back({i, m0});
}
}
} else {
FOR(i, 1, n) {
// if (root(i) == root(t[i])) continue;
// fa[root(i)] = root(t[i]);
WARN(i, t[i]);
elist[id].push_back({i, t[i]});
// elist[id].push_back({t[i], i});
}
}
}
FOR(i, 1, q) {
int a = input(), b = input(), c = input();
// INFO(a, b, c);
qlist[c].push_back({a, b, i});
}
FOR(i, 1, n) {
G[i].set(i);
H[i].set(i);
}
FOR(i, 0, l) {
for (auto [u, v] : elist[i]) {
add(u, v);
}
for (auto [a, b, id] : qlist[i]) {
ans[id] = G[a].test(b);
}
}
// SETCOLOR(BLUE);
FOR(i, 1, q) {
cout << ans[i];
}
cout << endl;
// SETCOLOR(RESET);
for (int i = 0; i <= n; i++) {
G[i].reset();
inq[i].reset();
H[i].reset();
}
for (int i = 0; i <= l; i++) {
elist[i].clear();
qlist[i].clear();
}
}
int main() {
// ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}