#include<bits/stdc++.h>
#define int long long
using namespace std;
int read() {
	int x=0, w=1; char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') w=-1; ch=getchar();}
	while(isdigit(ch)) {x=x*10+(ch-'0'); ch=getchar();}
	return x*w;
}
const int N=1e6+10;
const int P=1e9+7;
int n, m, v;
map<int, int> a;
int qpow(int a, int b) {
	int ans=1;
	while(b) {
		if(b&1) ans=ans*a%P;
		a=a*a%P; b>>=1;
	}
	return ans;
}
signed main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T; T=read();
	while(T--) {
		n=read(); m=read(); v=read();
		//for(int i=1;i<=n;i++) a[i]=0;
		a.clear();
		bool pd=0;
		for(int i=1;i<=m;i++) {
			int p=read(), c=read();
			a[p]=c; if(c!=1) pd=1;
		}	
		if(m==1) {
			printf("%lld\n", qpow(v*v%P, (n-1)));
			continue;
		}	
		if(m==n) {
			int c=(1+(v-1)*v%P)%P;
			int ans=qpow(c, n-1);
			printf("%lld\n", ans%P);
			continue;
		} 
		if(!pd) {
			int ans=1, le=1;
			for(auto t:a) {
				ans=ans*qpow(v*v%P, (t.first-le))%P;
				le=t.first+1;
			}
			ans=ans*qpow(v*v%P, (n-le+1))%P;
			printf("%lld\n", ans);
			continue;
		}
		printf("%lld\n", qpow(v*v%P, (n-m)));
	}
	return 0;
} 
//11:15 8pts
//11:25 16pts
//11:33 28pts
//11:41 ·ÅÆú 
