#include<bits/stdc++.h>
using namespace std;
priority_queue<long long,vector<long long>,greater<long long>> qu;
long long n,v,T,m,k,i,c,d,t,l,ans,nu,flag,st;
long long con[1000005];
long long ksm(long long a,long long b){
	long long kss;
	if(b==0)return 1;
	if(b==1)return a%1000000007;
	if(b%2==0){
		kss=ksm(a,(b>>1));
		return (kss*kss)%1000000007;
	}else{
		kss=ksm(a,(b>>1));
		return (((kss*kss)%1000000007)*a)%1000000007;
	}
	return 0;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	for(int q=1;q<=T;q++){
		while(!qu.empty())qu.pop();
		flag=0;
		ans=1;
		memset(con,0,sizeof(con));
		cin>>n>>m>>v;
		for(i=1;i<=m;i++){
			cin>>c>>d;
			if(!con[c]){
				con[c]=d;
			}else{
				if(con[c]==d){
					continue;
				}else{
					cout<<0<<'\n';
					flag=1;
				}
			}
			qu.push(c);
		}
		if(flag==1)continue;
		t=0;
		while(!qu.empty()){
			k=qu.top();
			qu.pop();
			if(t==0){
				st=k;
				t=k;
				continue;
			}
			l=k-t;
			nu=ksm(v,2*l)-ksm(v,l-1)*(v-1);
			nu=nu%1000000007;
			ans=(ans*nu)%1000000007;
			t=k;
		//	cout<<k<<' ';
		}
		ans*=ksm(v,2*(n-t))%1000000007;
		ans=ans%1000000007;
		ans*=ksm(v,2*(st-1))%1000000007;
		ans=ans%1000000007;
		cout<<ans<<'\n';
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
