#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include <bits/stdc++.h>
using namespace std;
#define For(i,n) for (int i=1;i<=(int)(n);i++)
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define all(x) (x).begin(),(x).end()
#define fore(i,x) for (auto &i:x)
#define pii pair<int,int>
#define vi vector <int>
#define pb push_back
#define ll long long
#define endl '\n'
const int MOD = 998244353;
const int INF = 0x7fffffff;
const int MAXN = 2000 + 3;
int n, c, q;
int tr[MAXN][MAXN];
int a[MAXN][MAXN];
#define tr(j) ((s[2*j]-48)*50+s[2*j+1]-48)
void solve() {
cin >> n >> c >> q;
rep(i, n + 2) {
rep(j, n + 2) {
a[i][j] = INF;
}
a[i][i] = 0;
}
For(i, c) {
string s;
cin >> s;
unordered_set <int> st;
bool ok = 1;
int con = -1;
For(j, n) {
int t = tr((j-1));
tr[i][j] = t;
// cout << a[i][j] << ",";
if (st.count(t)) {
if (con != -1) {
ok = 0;
break;
}
con = t;
}
st.insert(t);
}
// if (!ok) cout << "@";
if (ok) {
if (con == -1) {
// cout << "!";
For(j, n) {
a[j][tr[i][j]] = min(a[j][tr[i][j]],i);
}
} else {
vi v;
unordered_set<int> dst;
For(j, n) {
dst.insert(j);
}
For(j, n) {
dst.erase(tr[i][j]);
if (tr[i][j] == con) v.push_back(j);
}
int last = *dst.begin();
a[v[0]][last] = min(a[v[0]][last],i);
a[v[1]][last] = min(a[v[1]][last],i);
}
}
}
// cout << "!!!" << d[5][1] << endl;
for (int i = 1;i <= n;i++) {
queue<int> q[MAXN];
vi vis(MAXN);
fore(j, vis) j = INF;
// vis[i] = 0;
q[0].push(i);
// for (int j = 1;j <= n;j++) {
// if (d[i][j] != INF) {
// q[d[i][j]].push(j);
// }
// }
for (int lm = 0;lm <= n;lm++) {
while (!q[lm].empty()) {
int j = q[lm].front();
q[lm].pop();
if (vis[j]<lm) continue;
vis[j] = lm;
a[i][j] = min(a[i][j],lm);
For(k, n) {
if (a[j][k] != INF && max(lm, a[j][k])<vis[k]) {
q[max(lm, a[j][k])].push(k);
vis[k] = max(lm, a[j][k]);
}
}
}
}
}
rep(i, q) {
string s;
cin >> s;
int st, ed, lm;
st = tr(0);
ed = tr(1);
lm = tr(2);
// cout << endl<<st << " " << ed << " " << lm << " " << d[st][ed] << endl<<endl;
if (a[st][ed] <= lm) cout << "1"; else cout << "0";
}
cout << endl;
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}