#include<bits/stdc++.h>
using namespace std;
const int N=2e5;
int dp[101][N+1];
int n,k,q;
void solve(){
cin>>n>>k>>q;
vector<vector<int>>a(n+1);
vector<int>sz(n+1);
for(int i=1;i<=n;i++){
cin>>sz[i];
a[i].resize(sz[i]+1);
for(int j=1;j<=sz[i];j++){
cin>>a[i][j];
}
}
int R=100;
dp[0][1]=0;
for(int round=1;round<=R;round++){
for(int i=1;i<=n;i++){
int l=0,pre=-1;
for(int j=1;j<=sz[i];j++){
int val=a[i][j];
if(!(pre==-1 || prei)){
if(l+k-1>=j){
if(dp[round][val]-1){
dp[round][val]=i;
}else if(dp[round][val]!=i){
dp[round][val]=0;
}
}
}
int now=dp[round-1][a[i][j]];
if(now!=-1 && (now==0 || now!=i)){
pre=now;
l=j;
}
}
}
}
while(q--){
int r,c;
cin>>r>>c;
if(dp[r][c]>=0){
cout<<1<<endl;
}else{
cout<<0<<endl;
}
}
}
int main(){
int t;
cin>>t;
while(t--){
for(int i=0;i<=100;i++){
for(int j=1;j<=N;j++){
dp[i][j]=-1;
}
}
solve();
}
return 0;
}