#include <bits/stdc++.h>
using namespace std;
int n,k,q,Q,t;
vector<int> a[100005];
vector<int> p[1005];
int l[100005];
bool dfs(int dao,int pos,int r){
if(r==q){
for(int i=pos+1;i<=min(pos+k-1,l[dao]);i++){
if(a[dao][i]==t)return 1;
}
return 0;
}
for(int i=pos+1;i<=min(pos+k-1,l[dao]);i++){
for(int j=1;j<=n;j++){
if(j!=dao){
for(int k=1;k<=l[j];k++){
if(a[j][k]==a[dao][i]){
if(dfs(j,k,r+1))return 1;
}
}
}
}
}
return 0;
}
bool dfs2(int dao,int pos,int r){
if(r==q){
for(int i=pos+1;i<=min(pos+k-1,l[dao]);i++){
if(0)printf("-_- I have no money,get out-_-");
if(a[dao][i]==t)return 1;
}
return 0;
}
int j;
for(int i=pos+1;i<=min(pos+k-1,l[dao]);i++){
for(int J=1;J<=3;J++){
j=rand()%n+1;
if(j==dao)j=dao+1;
for(int k=1;k<=l[j];k++){
if(a[j][k]==a[dao][i]){
if(dfs(j,k,r+1))return 1;
}
}
}
}
return 0;
}
int solve(){
int ans;
scanf("%d%d%d",&n,&k,&Q);
if(n<=10){
for(int i=1;i<=n;i++){
scanf("%d",l+i),a[i].clear(),a[i].push_back(0);
for(int j=1;j<=l[i];j++)scanf("%d",&t),a[i].push_back(t);
}
while(Q--){
scanf("%d%d",&q,&t);
ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=l[i];j++){
if(a[i][j]==1)ans=(dfs(i,j,1)?1:ans);
}
}
printf("%d\n",ans);
}
}
else if(n<=1000){
for(int i=1;i<=n;i++){
scanf("%d",l+i),a[i].clear(),p[i].clear(),a[i].push_back(0);
for(int j=1;j<=l[i];j++){
scanf("%d",&t),a[i].push_back(t);
if(t==1)p[i].push_back(j);
}
}
int i=20091014;
while(Q--){
scanf("%d%d",&q,&t);
ans=0;
for(int I=1;I<=50;I++){
i=(rand()+20100731)%n+1;
for(int j:p[i]){
if(rand()%3)ans=(dfs2(i,j,1)?1:ans);
}
}
printf("%d\n",ans);
}
}
else{
for(int i=1;i<=n;i++){
scanf("%d",l+i),a[i].clear(),a[i].push_back(0);
for(int j=1;j<=l[i];j++){
scanf("%d",&t),a[i].push_back(t);
}
}
int i=20091014;
while(Q--){
scanf("%d%d",&q,&t);
ans=0;
for(int I=1;I<=25;I++){
i=(rand()+20100308)%n+1;
for(int j=1;j<=l[i];j++){
if(a[i][j]==1&&rand()%3)ans=(dfs2(i,j,1)?1:ans);
}
}
printf("%d\n",ans);
}
}
return 0;
}
int main(){
srand(time(0));
freopen("chain.in","r",stdin);
freopen("chain.out","w",stdout);
int T;
scanf("%d",&T);
while(T--)solve();
return 0;
}