#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fi first
#define se second
using ll = long long;
using VI = vector<int>;
using PII = pair<int, int>;
using VPII = vector<PII>;
const int inf = 1 << 30;
void solve() {
int n, l, m;
cin >> n >> l >> m;
VI ans(m + 2), fa(n + 2), to(n + 2), cnt(n + 2);
vector<VPII> e(n + 2);
function<int(int)> find = [&](int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
};
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
for (int i = 1; i <= l; i++) {
string s;
cin >> s;
for (int j = 1, k = 0; j <= n; k += 2, j++) {
to[j] = (s[k] - 48) * 50 + (s[k + 1] - 48);
}
for (int j = 0; j <= n; j++) {
cnt[j] = 0;
}
for (int j = 1; j <= n; j++) {
cnt[0] += !cnt[to[j]]++;
}
if (cnt[0] == n) {
for (int j = 1; j <= n; j++) {
int u = find(j), v = find(to[j]);
if (u != v) {
fa[v] = u;
e[u].pb({v, i});
e[v].pb({u, i});
}
}
} else if (cnt[0] == n - 1) {
int s, t;
for (int j = 1; j <= n; j++) {
if (!cnt[j]) {
t = j;
} else if (cnt[j] == 2) {
s = j;
}
}
for (int j = 1; j <= n; j++) {
if (to[j] == s) {
e[j].pb({t, i});
}
}
}
}
vector<queue<int>> q(l + 2);
vector dis(n + 2, VI(n + 2, inf));
VI vis(n + 2);
for (int i = 1; i <= n; i++) {
dis[i][i] = 0;
q[0].push(i);
fill(vis.begin(), vis.end(), 0);
for (int j = 0; j <= l; j++) {
while (!q[j].empty()) {
int x = q[j].front();
q[j].pop();
if (vis[x]) continue;
vis[x] = 1;
for (auto [y, w]: e[x]) {
w = max(w, dis[i][x]);
if (w < dis[i][y]) {
dis[i][y] = w;
q[w].push(y);
}
}
}
}
}
for (int i = 1; i <= m; i++) {
string s;
cin >> s;
int u = (s[0] - 48) * 50 + (s[1] - 48);
int v = (s[2] - 48) * 50 + (s[3] - 48);
int w = (s[4] - 48) * 50 + (s[5] - 48);
cout << "01"[dis[u][v] <= w];
}
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tc = 1;
cin >> tc;
while (tc--) {
solve();
}
return 0;
}