#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5,mod=1e9+7; 
int t,n,m,v,ans;
int fpow(int a,int n){
	int ans=1;
	while(n){
		if(n&1) ans=ans*a%mod;
		a=a*a%mod,n>>=1;
	}
	return ans;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld%lld",&n,&m,&v);
		ans=1;map<int,int>mp;
		bool flag=1;
		for(int i=1;i<=m;i++){
			int c,d;
			scanf("%lld%lld",&c,&d);
			if(!mp[c]) mp[c]=d;
			else if(mp[c]!=d){
				flag=0;
				break;
			}
		}
		if(!flag){
			printf("0\n");
			continue;
		}
		int l=1;
		for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
			int r=it->first;
			ans=ans*(fpow(v,(r-l)<<1)-(flag?0:fpow(v,r-l-1)*(v-1)%mod))%mod;
			l=r,flag=0;
		}
		ans=ans*fpow(v,(n-l)<<1)%mod;
		ans=(ans+mod)%mod;
		printf("%lld\n",ans);
	}
	
	return 0;
}
