#include<bits/stdc++.h>
typedef unsigned long long ull;
using namespace std;
const ull MOD=1e9+7;
ull T,n,m,v,c,d;ull ans;
ull dv[(int)1e7+50];
bool b[(int)1e7+50],t;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--){
		ans=1;t=true;
		memset(dv,0,sizeof(dv));memset(b,0,sizeof(b));
		cin>>n>>m>>v;
		v=v%MOD;
		for(int i=1;i<=m;i++){
			scanf("%lld%lld",&c,&d);
			if(dv[c]!=d&&dv[c]!=0){
				t=false;break;
			}
			dv[c]=d;b[c]=true;
		}
		if(!t){
			printf("0\n");continue;
		}
		for(int i=2;i<=n;i++){
			ans=(ans*v)%MOD;ans=(ans*v)%MOD;
			if(b[i]&&b[i-1])ans=ans-v+1;
		}
		cout<<ans%MOD<<endl;
	}
	return 0;
} 
