#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,M=1e9+7;
int t,n,m,v;
/*
typedef pair<int,int> PII;
#define fi first
#define se second
PII a[N];
*/
typedef long long ll;
ll ans;
int c[N];
map<int,int> mp;
ll qmi(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1)res=res*a%M;
		a=a*a%M;
		b>>=1;
	}
	return res;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	
	while(t--){
		cin>>n>>m>>v;
		mp.clear();
		if(v==1){
			cout<<1<<endl;	
		}
		else{
			ans=1;
			for(int i=1;i<=m;i++){
				//cin>>a[i].fi>>a[i].se;
				cin>>c[i];
				int d;
				cin>>d;
				if(mp.count(c[i])&&mp[c[i]]!=d){
					ans=0;
					break;
				}
				mp[c[i]]=d;
			}
			if(!ans){
				cout<<ans<<endl;
				continue;
			}
			sort(c+1,c+1+m);
			int mm=unique(c+1,c+1+m)-c-1;
			//特判重复的位置
			if(mm<=1){
				cout<<qmi(v,2*(n-1))<<endl;
				continue;
			}
			if(c[1]>1)ans=qmi(v,2*(c[1]-1));//则1~x1 -1 可以任取 
			for(int i=2;i<=mm;i++){
				(ans*=((qmi(v,2*(c[i]-c[i-1]))-(v-1)*qmi(v,c[i]-c[i-1]-1)%M)+M)%M)%=M;
			}
			if(c[mm]<n-1)(ans*=qmi(v,2*(n-c[mm])))%=M;
			cout<<ans<<endl;
		}
		
	}
}
