#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
#define MP make_pair
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define int long long
typedef long long ll;
using namespace std;

int read(){
	int s=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9')f^=(c=='-'),c=getchar();
	while(c>='0'&&c<='9')s=s*10+c-'0',c=getchar();
	return f?s:-s;
}

const int N=100005,mod=1e9+7;
int fpow(int x,int k){
	int res=1;
	for(;k;k>>=1){
		if(k&1)res=res*x%mod;
		x=x*x%mod;
	}
	return res;
}
int n,m,V,pw[N],ans,top;
int lsh[N];
map<int,int>vis;

signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int now=0;
	for(int T=read();T--;){
		n=read(),m=read(),V=read();
		bool flag=0;
		top=0;
		map<int,int>().swap(vis);
		rep(i,1,m){
			int c=read(),d=read();
			if(vis.count(c)&&vis[c]!=d)flag=1;
			vis[c]=d;
			lsh[i]=c;
		}
		if(flag){
			printf("0\n");
			continue;
		}
		sort(lsh+1,lsh+m+1);
		m=unique(lsh+1,lsh+m+1)-lsh-1;
		ans=1;
		rep(i,2,m){
			ans=ans*(fpow(V*V%mod,lsh[i]-lsh[i-1])-fpow(V,lsh[i]-lsh[i-1]-1)*(V-1)%mod+mod)%mod;
		}
		ans=ans*fpow(V*V%mod,lsh[1]-1)%mod*fpow(V*V%mod,n-lsh[m])%mod;
		printf("%lld\n",ans);
	}
	return 0;
}
