#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5,mod=1e9+7;
int read(){int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f; 
}
int ksm(int a,int b){int s=1;for(;b;b>>=1,a=1ll*a*a%mod)if(b&1)s=1ll*s*a%mod;return s;}
void qm(int &x,int y){x+=y;if(x>=mod)x-=mod;}
int T,n,m,v,a[N];
int f[N][2];
struct Mat{int a,b,c,d;
	Mat(){a=b=c=d=0;}
	Mat operator * (Mat &x)const{
		Mat y;
		qm(y.a,1ll*a*x.a%mod);
		qm(y.a,1ll*b*x.c%mod);
		qm(y.b,1ll*a*x.b%mod);
		qm(y.b,1ll*b*x.d%mod);
		qm(y.c,1ll*c*x.a%mod);
		qm(y.c,1ll*d*x.c%mod);
		qm(y.d,1ll*c*x.b%mod);
		qm(y.d,1ll*d*x.d%mod);
		return y;
	}
}bs[35];
map<int,int> mp;
void solve(){mp.clear();bool p=0;
	n=read(),m=read(),v=read();
	bs[0].a=v;
	bs[0].b=1ll*(v-1)*v%mod;
	bs[0].d=1ll*v*v%mod;
	for(int i=1;i<=31;i++)bs[i]=bs[i-1]*bs[i-1];
	for(int i=1,x,y;i<=m;i++){
		x=read(),y=read();
		if(mp.count(x)&&mp[x]!=y)p=1;
		mp[x]=y;
	}
	if(p)return puts("0"),void();
	int cnt=0;a[0]=1;
	f[1][1]=f[1][0]=0;
	Mat G;
	if(mp.count(1))G.a=1;else G.b=1;
	for(auto it:mp)if(it.first!=1)a[++cnt]=it.first;
	for(int i=1;i<=cnt;i++){
		Mat g;g.a=g.d=1;
		int d=a[i]-a[i-1]-1;
		for(int i=0;d;d>>=1,i++)if(d&1)g=g*bs[i];
		G=G*g;
		int x=G.a,y=G.b;G.a=G.b=0;
		G.a=x;
		qm(G.a,1ll*x*(v-1)%mod*v%mod);
		qm(G.a,1ll*y*v%mod*v%mod);
	}
	if(!mp.count(n)){
		Mat g;g.a=g.d=1;
		int d=n-a[cnt];
		for(int i=0;d;d>>=1,i++)if(d&1)g=g*bs[i];
		G=G*g;
	}
	printf("%d\n",(G.a+G.b)%mod);
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--)solve();
	return 0;
}

