#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

inline long long read(){
	register long long x=0;
	register char ch=getchar();
	while(ch<'0'||ch>'9'){
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x;
}

const long long MOD=1000000007ll;

long long power(long long x,long long index){
	long long ret=1;
	while(index){
		if(index%2){
			ret*=x;
			ret%=MOD;
		}
		index/=2;
		x*=x;
		x%=MOD;
	}
	return ret;
}

struct node{
	int c,d;
}arr[100005];

bool cmd(node a,node b){
	return (a.c<b.c);
}

long long T,n,m,v,c[100005],dp[100005];

int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=read();
	while(T--){
		n=read(),m=read(),v=read();
		for(int i=1,d;i<=m;++i){
			arr[i].c=read(),arr[i].d=read();
		}
		sort(arr+1,arr+m+1,cmd);
		int st=0,ct=0;
		for(int i=1,p=1;i<=m;++i){
			if(arr[i].c!=arr[i-1].c){
				st=arr[i].d;
				c[p]=arr[i].c;
				++p;
			}else{
				if(arr[i].d!=st){
					printf("0\n");
					ct=-1;
					break;
				}
				++ct;
			}
		}
		if(ct==-1){
			continue;
		}
		m-=ct;
		dp[1]=power(v,2*(c[1]-1));
		for(int i=2;i<=m;++i){
			dp[i]=(dp[i-1]*((power(v,2*(c[i]-c[i-1]))-((power(v,c[i]-c[i-1]-1)*(v-1))%MOD)+MOD)%MOD))%MOD;
		}		
		printf("%lld\n",(dp[m]*power(v,2*(n-c[m])))%MOD);
	}
	return 0;
}
