#include<bits/stdc++.h>
#define int long long
const int mod=1e9+7;
using namespace std;
int t,n,m,v,c[100005],d[100005],num[10000005],ans;
bool f;
void fc(int k,int la,int cnt){
	if(k==n){
		ans+=cnt;
		ans%=mod;
		return;
	}
	if(la==0){
		if(num[k]!=0){
			if(num[k+1]==0)fc(k+1,1,cnt*2);
			else fc(k+1,1,cnt);
		}else fc(k+1,0,cnt*4);
	}else{
		if(num[k+1]==0)fc(k+1,1,cnt*2);
		else fc(k+1,1,cnt);
		fc(k+1,0,cnt*2);
	} 
	return;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--){
		cin>>n>>m>>v;
		f=false;
		for(int x=1;x<=m;x++){
			cin>>c[x]>>d[x];
			if(c[x]!=x)f=true;
		}
		if(f==false&&m==n){
			ans=1;
			for(int x=1;x<n;x++){
				ans=(ans*((v*(v-1))%mod+1)%mod)%mod;
			}
	 		cout<<ans<<"\n";
			continue;
		}
		if(v==2&&n<=30){
			fill(num,num+n+1,0);
			for(int x=1;x<=m;x++){
				if(num[c[x]]==0)num[c[x]]=d[x];
				else{
					cout<<"0\n",num[0]=-1;
					break;
				}
			}
			if(num[0]==-1)continue;
			ans=0;
			fc(1,num[1],1);
			cout<<(ans%mod)<<"\n";
			continue;
		}
		cout<<"0\n";
	}
	return 0;
}
