#include<bits/stdc++.h>
using namespace std;
const int Mod=1e9+7;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin>>T;
	while(T--){
		int n,m,v;
		cin>>n>>m>>v;
		int c,d;
		vector<int> nums(n,0);
		map<int,int> Map;
		bool flag1=false;
		for(int i=0;i<m;i++){
			cin>>c>>d;
			if(Map.count(c) && Map[c]!=d){
				cout<<0<<endl;
				flag1=true;
				break;
			}
			Map[c]=d;
			nums[c]=d;
		}
		if(flag1)	continue;
		if(m==n){
			cout<<1<<endl;
			continue;
		}
		if(v==1){
			cout<<1<<endl;
			continue;
		}
		if(v==2){
			cout<<0<<endl;
			continue; 
		}
		cout<<0<<endl;
	}
	return 0;
}
