#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1000000007,maxm=100010;
map<int,int>mp;
map<int,bool>flag;
struct node{
	int c,d;
	bool operator<(const node t)const{
		return c<t.c;
	}
}a[maxm];
int ksm(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
void solve(){
	int n,m,v;
	scanf("%lld%lld%lld",&n,&m,&v);
	mp.clear();
	flag.clear();
	for(int i=1;i<=m;i++){
		int c,d;
		scanf("%lld%lld",&c,&d);
		a[i]=(node){c,d};
		if(flag[c]&&mp[c]!=d){
			printf("0\n");
			return ;
		}
		mp[c]=d;
		if(flag[c]==0) flag[c]=1;
	}
	sort(a+1,a+1+m);
	int ans=ksm(v*v%mod,n-1);
	for(int i=2;i<=m;i++){
		int c=a[i].c;
		if(c==a[i-1].c) continue;
		int z=ksm(v*v%mod,c-a[i-1].c),b=ksm(v,c-a[i-1].c-1)*(v-1)%mod;
		ans=ans*ksm(z,mod-2)%mod*((z-b+mod)%mod)%mod;
	}
	printf("%lld\n",ans);
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t;
	scanf("%lld",&t);
	while(t--) solve();
	return 0;
} 
