#include<bits/stdc++.h>
using namespace std;
#define int long long
using PII=pair<int,int>;
#define File(str) \
freopen(str".in","r",stdin);\
freopen(str".out","w",stdout)
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define irep(i,x,y) for(int i=x;i>=y;--i)
#define Set(x,y) memset(x,y,sizeof(x))
#define fi first
#define se second
#define pb push_back
#define PQ priority_queue
#define TLC (double)clock()/CLOCKS_PER_SEC<0.91
#define W while
#define Dbg(fmt,...) \
fprintf(stderr,"%s %d %s: " fmt "\n",\
__FILE__,__LINE__,__FUNCTION__,##__VA_ARGS__)
void smin(int&a,int b){a=min(a,b);}
void smax(int&a,int b){a=max(a,b);}
const int N=2010,Mod=1e9+7,V=2e5+10;
int n,K,q,tot,L[N],a[N],R[N],bel[N];
bool dp[110][N];
vector<int> G[V];
bool in(int x,int l,int r){
rep(i,l,r)if(a[i]==x)return 1;
return 0;
}
void Init(){
Set(L,0),Set(R,0),Set(bel,0),Set(dp,0),Set(a,0);
cin>>n>>K>>q;
tot=0;
rep(i,1,n){
int l;
L[i]=tot+1;
cin>>l;
while(l--)cin>>a[++tot],bel[tot]=i,G[a[tot]].pb(tot);
R[i]=tot;
}
}
void Solve(){
Init();
rep(i,1,tot)
if(a[i]==1){
for(int j=i+1;j<=R[bel[i]];++j)
dp[1][j]=1;
i=R[bel[i]];
}
rep(i,2,100){
rep(j,1,tot)
rep(k,1,tot)
if(dp[i-1][k]&&bel[j]!=bel[k]&&in(a[k],max(L[bel[j]],j-K+1),j-1)){
dp[i][j]=1;
break;
}
}
W(q--){
int r,c;
cin>>r>>c;
for(int x:G[c]){
if(dp[r][x]){
cout<<"1\n";
goto End;
}
}
cout<<"0\n";
End:;
}
}
main(){
cin.tie(0)->sync_with_stdio(0);
int T=1;
cin>>T;
while(T--)
Solve();
}