#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#define ll long long

const int MAXN=1e5+10;
const int modp=1e9+7;
using namespace std;
ll T,n,m,cnt=0;
ll v;
ll pos[MAXN]; 
map<ll,ll> vl;
map<ll,bool> exists;
set<ll> ex;

ll fst_pow(ll a,int p){
	ll ans=1;
	while(p){
		if(p%2){
			ans*=a;
			ans%=modp;
		}
		p/=2,a*=a,a%=modp;
	}
	
	return ans;
}

ll chk(ll k1,int s){
	ll ans=1;
	for(int i=1;i<=s;i++)
		ans=(ans*k1+1)%modp;
	return ans;
}

int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	
	while(T--){
		scanf("%lld%lld%lld",&n,&m,&v);
		
		cnt=0;
//		memset(exist,0,sizeof(exist));
		vl.clear();
		exists.clear();
		ll temp1=(v*v)%modp;
//		cout<<temp1<<" "<<temp2<<endl;
		bool flag=false;
		for(int i=1;i<=m;i++){
			ll c,d;
			scanf("%lld%lld",&c,&d);
			if( exists[c] ){
				if( d!=vl[c] ) flag=true;
				continue;
			}
			pos[++cnt]=c;
			exists[c]=true;
			vl[c]=d;
		}
//		printf("%d %d\n",t1,t2);
		if(flag){
			printf("0\n");
			continue;
		}
		
		ll ans2=1;
		sort(pos+1,pos+cnt+1);
		for(int i=1;i<cnt;i++){
			int k=pos[i+1]-pos[i];
			ll alpha=fst_pow(v,k);
//			printf( "%lld\n" , (alpha*(alpha-1))%modp+fst_pow(v,k-1) );
			ans2*=( (alpha*(alpha-1))%modp + fst_pow(v,k-1) )%modp;
			ans2%=modp;
		}
//		for(int i=1;i<n;i++){
//			if(exists[i]&&exists[i+1]){
//				ans*=temp2;
//				ans%=modp;
//			}else{
//				ans*=temp1;
//				ans%=modp;
//			}
//		}
		ans2=(ans2*fst_pow(temp1,n-pos[cnt]+pos[1]-1))%modp;
		
		printf("%lld\n",ans2);
	}
	
	fclose(stdin);
	fclose(stdout);
	return 0;
} 
