#include <bits/stdc++.h>
using namespace std;
const long long p=1e9+7;
int tt,n,m;
long long ans,v;
struct node{
	int id,x;
}s[100010];
long long qpow(long long a,long long b){
	long long res=1;
	while(b){
		if(b&1) res=res*a%p;
		a=a*a%p;
		b>>=1;
	}
	return res;
}
bool cmp(const node a,const node b){
	return a.id<b.id;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>tt;
	while(tt--){
		scanf("%d%d%lld",&n,&m,&v);
		for(int i=1;i<=m;i++){
			scanf("%d%d",&s[i].id,&s[i].x);
		}
		sort(s+1,s+m+1,cmp);
		ans=qpow(v,2*s[1].id-2);
		for(int i=2;i<=m;i++){
			if(s[i].id==s[i-1].id){
				if(s[i].x!=s[i-1].x){
					ans=0;
					break ;
				}else continue ;
			}
			ans=(qpow(v,2*(s[i].id-s[i-1].id))+p-qpow(v,s[i].id-s[i-1].id-1)*(v-1)%p)%p*ans%p;
		}
		printf("%lld\n",ans*qpow(v,2*(n-s[m].id))%p);
	}
	return 0;
}

