#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#define int long long
using namespace std;
int read(){
	int res=0,fg=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')fg=-1;c=getchar();}
	while(c>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0',c=getchar();
	return res*fg;
}
void write(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
const int maxn=2e5+5;
const int mod=1e9+7;
int n,m,V;
int qpow(int x,int y){int res=1;for(;y;y>>=1,x=x*x%mod)if(y&1)res=res*x%mod;return res;}
map<int,int>mp;
int qwq[maxn],qcnt;
void solve(){
	n=read(),m=read(),V=read();mp.clear(),qcnt=0;
	bool fg=1;
	for(int i=1;i<=m;i++){
		int x=read(),y=read();
		if(mp[x]!=0&&mp[x]!=y)fg=0;
		else if(mp[x]==0)qwq[++qcnt]=x,mp[x]=y;
	}
	if(!fg){puts("0");return ;}
	sort(qwq+1,qwq+qcnt+1);
	int ans=1;
	ans=ans*qpow(V,(qwq[1]-1)*2)%mod;
	for(int i=2;i<=qcnt;i++){
		ans=ans*(qpow(V,(qwq[i]-qwq[i-1])*2)-qpow(V,(qwq[i]-qwq[i-1]-1))*(V-1)%mod+mod)%mod;
	}
	ans=ans*qpow(V,(n-qwq[qcnt])*2)%mod;
	write(ans),putchar('\n');
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T=read();
	while(T--)solve();
	return 0;
}
