#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5+5;
int T,n,k,q,l[N],dp[105][N];
vector<int>s[N<<1];
signed main(){
cin>>T;
while(T--){
cin>>n>>k>>q;
for(int i = 1; i<=n; i++){
cin>>l[i];
s[i].push_back(0);
for(int j = 1; j<=l[i]; j++){
int x;
cin>>x;
s[i].push_back(x);
}
}
dp[0][1]=-1;
for(int i = 1; i<=100; i++){
for(int j = 1; j<=n; j++){
int tmp = 0;
for(int x = 1; x<=l[j]; x++){
if(tmp>0){
tmp--;
if(dp[i][s[j][x]]==0){
dp[i][s[j][x]]=x;
}
else if(dp[i][s[j][x]]>0 && dp[i][s[j][x]]!=x){
dp[i][s[j][x]]=-1;
}
}
if(dp[i-1][s[j][x]]!=0 && dp[i-1][s[j][x]]!=x){
tmp=k-1;
}
}
}
}
for(int i = 1; i<=q; i++){
int r,c;
cin>>r>>c;
if(dp[r][c]!=0) {
cout<<1<<endl;
}
else{
cout<<0<<endl;
}
}
}
return 0;
}