#include<bits/stdc++.h>
using namespace std;

#define int long long
#define siz(x) ((int)(x).size()) 
#define me(x,v) memset(x,v,sizeof(x))
#define L(i,l,r) for(int i=(l);i<=(r);i++)
#define R(i,r,l) for(int i=(r);i>=(l);i--)
#define ve(x) vector<x>
#define pa(x,y) pair<x,y>
#define x first
#define y second

char gc(){
	return getchar();
}
void pc(char c){
	putchar(c);
}
void el(){
	pc('\n');
}
int qread(){
	int res=0;
	bool sgn=0;
	char c=gc();
	while(c<'0'||c>'9'){
		sgn|=(c=='-');
		c=gc();
	}
	while(c>='0'&&c<='9'){
		res=res*10+c-'0';
		c=gc();
	}
	return sgn?-res:res;
}

constexpr int mod=1e9+7;
int qpow(int a,int b){
	int res=1;
	for(;b;b>>=1,a=a*a%mod){
		if(b&1){
			res=res*a%mod;
		}
	}
	return res;
}

constexpr int N=114514*10;

struct R{
	int c,d;
};
//
//bool cmp(const R& x,const R& y){
//	return x.c<y.c;
//}

int n,m,v;
R inp[N],r[N];

int rc[N],rcn;
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	
	int T=qread();
	while(T--){
		me(r,0);
		rcn=0;
		
		n=qread(),m=qread(),v=qread();
			
		L(i,1,m){
			inp[i].c=qread();
			inp[i].d=qread();
			rc[++rcn]=inp[i].c;
		}
		
		sort(rc+1,rc+1+rcn);
		rcn=unique(rc+1,rc+1+rcn)-(rc+1);
		
		bool fail=0;
		L(i,1,m){
			int p=lower_bound(rc+1,rc+1+rcn,inp[i].c)-rc;
			r[p].c=inp[i].c;
			if(r[p].d&&r[p].d!=inp[i].d){
				fail=1;
				break;
			}
			r[p].d=inp[i].d;
		}
		
		if(fail){
			puts("0");
			continue;
		}
		
//		L(i,1,rcn){
//			printf("c %lld d %lld\n",r[i].c,r[i].d);
//		}
//		el();
		
		int ans=qpow(v,2*(r[1].c-1));
		L(i,1,rcn-1){
			if(r[i].c==r[i+1].c-1){
				int nw=((v-1)*v%mod+1)%mod;
				ans=ans*nw%mod;
			}else{
				int nw=(qpow(v,2*(r[i+1].c-r[i].c))-qpow(v,r[i+1].c-r[i].c)+qpow(v,r[i+1].c-r[i].c-1)+mod)%mod;
				ans=ans*nw%mod;
			}
		}
		
		ans*=qpow(v,2*(n-r[rcn].c));
		ans%=mod;
		
		printf("%lld\n",ans); 
	}
	return 0;
} 
