#include<fstream>
#include<vector>
std::ifstream cin("chain.in");
std::ofstream cout("chain.out");
void solve(){
	int n,k,q;
	cin>>n>>k>>q;
	std::vector< std::vector<int> > s(n+1);
	std::vector< std::vector<int> > ans(101,std::vector<int>(200001));
	for(int i=1;i<=n;i++){
		int k;
		cin>>k;
		s[i].resize(k);
		for(int j=0;j<k;j++) cin>>s[i][j];
	}
	ans[0][1]=-1;
	for(int r=1;r<=100;r++){
		for(int i=1;i<=n;i++){
			int cnt=0;
			for(auto u:s[i]){
				if(cnt>0){
					if(ans[r][u]==0) ans[r][u]=i;
					else if(ans[r][u]!=i) ans[r][u]=-1;
					--cnt;
				}
				if(ans[r-1][u]&&ans[r-1][u]!=i) cnt=k-1;
			}
		}
	}
	while(q--){
		int r,c;
		cin>>r>>c;
		cout<<!!ans[r][c]<<'\n';
	}
}
int main(){
	int T;
	cin>>T;
	while(T--) solve();
	return 0;
}
