#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,k,q,l,x;
int cnt[105][200005];
vector<int> v[200005];
void dfs(){
memset(cnt,-1,sizeof cnt);
cin>>n>>k>>q;
for(int i=1;i<=n;i++){
v[i].clear();
cin>>l;
for(int j=1;j<=l;j++){
cin>>x;
v[i].push_back(x);
}
}
cnt[0][1]=0;
for(int t=1;t<=100;t++){
for(int i=1;i<=n;i++){
int len=0;
for(auto x : v[i]){
if(len>0){
if(cnt[t][x]==-1){
cnt[t][x]=i;
} else if(cnt[t][x]!=i){
cnt[t][x]=0;
}
len--;
}
if(cnt[t-1][x]>=0 && cnt[t-1][x]!=i){
len=k-1;
}
}
}
}
while(q--){
int r,c;
cin>>r>>c;
cout<<(cnt[r][c]!=-1 ? 1 : 0) <<endl;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--) dfs;
return 0;
}