#include<bits/stdc++.h>
#define int long long
#define mod 1000000007ll
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define mems(a,x) memset(a,x,sizeof(a))
using namespace std;
const int maxn=200010;
const int inf=1e18;
inline int read(){
	int x=0,fl=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')fl=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch-'0');ch=getchar();}
	return x*fl;
}
bool mbe;

int n,m,v,ans;
int a[maxn];
inline int ksm(int a,int b=mod-2){
	int ans=1;
	while(b){
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
map<int,int> mp;
void work(){
	n=read();m=read(),v=read();
	mp.clear();
	bool fl=0;
	for(int i=1;i<=m;i++){
		a[i]=read();int val=read();
		if(mp[a[i]]&&mp[a[i]]!=val)fl=1;
		mp[a[i]]=val;
	}
	if(fl){puts("0");return ;}
	sort(a+1,a+m+1);m=unique(a+1,a+m+1)-a-1;
	ans=ksm(v,2*(a[1]-1));
	// for(int i=1;i<=m;i++)cout<<a[i]<<" ";cout<<"\n";
	for(int i=2;i<=m;i++){
		int val=ksm(v,2*(a[i]-a[i-1]));
		(val+=mod-ksm(v,a[i]-a[i-1]-1)*(v-1)%mod)%=mod;
		ans=ans*val%mod;
		// cout<<i<<" "<<val<<"\n";
	}
	ans=ans*ksm(v,2*(n-a[m]))%mod;
	printf("%lld\n",ans);
}

bool med;
int T;
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	
	// cerr<<(&mbe-&med)/1024.0/1024.0<<"\n";
	
	T=read();
	while(T--)work();
}