#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
long long f[N][2],n,m,v,x[N]; 
const int mod=1e9+7;
// 0 yushu 1 bushou
void Main(){
	cin>>n>>m>>v;
	memset(x,0,sizeof(x));
	memset(f,0,sizeof(f));
	bool flag=0;
	for(int i=1;i<=m;i++){
		int c,d;
		cin>>c>>d;
		if(n>N)continue;
		if(x[c]!=0&&x[c]!=d){
			flag=1;
		}
		x[c]=d;
//		cout<<" "<<c<<endl;
	}
	if(n>=N){
//		cout<<" "<<n<<endl;
		cout<<0<<endl;
		return ;
	}
	f[1][0]=1;
	if(x[1]){
		f[1][0]=0;
		f[1][1]=1;
	}
	if(flag){
		cout<<0<<endl;
		return ;
	}
	for(int i=1;i<n;i++){
		if(x[i+1]){
			f[i+1][1]=(f[i][0]*v)%mod*v%mod+f[i][1]*(v-1)%mod*v%mod+f[i][1];
			f[i+1][1]%=mod;
		}
		else{
			f[i+1][1]=f[i][1]*v%mod;
			f[i+1][0]=f[i][1]*(v-1)%mod*v%mod+f[i][0]*v%mod*v%mod;
			f[i+1][0]%=mod;
		}
	}
	cout<<f[n][0]+f[n][1]<<"\n";
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin>>T;
	while(T--){
		Main();
	}
	return 0;
}

