#include <iostream>
#include <cstring>
using namespace std;

const int N = 1e3 + 5;

int maxn, r[100000], v[100000];
bool vis[105][200005];
vector<int> a[N], st;

void dfs(int x, int y, int dep) {
	if(dep > maxn) return;
	vis[dep][a[x][y]] = 1;
	for(int i = x + 1; i <= min(a[x][0], x + k); i++) {
		for(int j = 1; j <= n; j++) {
			for(int k2 = 1; k2 <= a[j][0]; k2++) {
				
			}
		} 
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while(t--) {
		int n, k, q;
		cin >> n >> k >> q;
		st.clear();
		for(int i = 1; i <= n; i++) a[i].clear();
		for(int i = 1; i <= n; i++) {
			int l;
			cin >> l;
			a[i].push_back(l);
			for(int j = 1; j <= l; j++) {
				int x;
				cin >> x;
				if(x == 1 && st.size() == 0 || st[st.size - 1] != i) st.push_back(i);
				a[i].push_back(x);
			}
		}
		maxn = 0;
		for(int i = 1; i <= q; i++) {
			cin >> r[i] >> c[i];
			maxn = max(maxn, [i]);
		}
		for(int i = 0; i < st.size(); i++) {
			for(int j = 1; j < a[st[i]][0]; j++) {
				if(a[st[i]][j] == 1) dfs(i, j, 0);
			}
		}
		for(int i = 1; i <= q; i++) cout << vis[r[i]][c[i]] << endl;
	}
	return 0;
}