#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
const long long M=1e9+7;
ll ksm(ll a,ll b,ll m){
	a%=m;
	b%=m;
	ll ans=1,x=a;
	while(b>0){
		if(b&1)ans=(ans*x)%m;
		x=(x*x)%m;
		b>>=1;
	}
	return ans%m;
}
ll t,n,m,k,a[100010];
struct N{
	ll y,v;
};
bool cmp(N a,N b){
	return a.y<b.y;
}
vector<N> v;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>t;
	while(t--){
		v.clear();
		memset(a,0,sizeof(a));
		cin>>n>>m>>k;
		if(n>1e6){
			cout<<0<<endl;
			continue;
		}
		k%=M;
		int fl=0;
		for(int i=1;i<=m;i++){
			ll y,vv;
			cin>>y>>vv;
			if(a[y]!=0 && a[y]!=vv){
				fl=1;
				cout<<0<<endl;
				break;
			}
			if(a[y]==0)v.push_back({y,vv}); 
			a[y]=vv;
		}
		if(fl)continue;
		if(v.size()==1){
			cout<<ksm(k*k,n-1,M)<<endl;
			continue;
		}
		sort(v.begin(),v.end(),cmp);
		ll ans=1,p=1;
//		for(N i:v){
//			cout<<i.y<<" ";
//		}
//		cout<<endl; 
		for(int i=0;i<v.size();i++){
//			cout<<endl<<ans<<endl<<endl;
			if(v[i].y-p>1){
				ans=(ans*ksm(k*k-k+3,v[i].y-p-1,M))%M;
				ans=(ans*((k*k+k+1)%M))%M;
			}
			else if(v[i].y-p==1){
				if(m==v.size())ans=(ans*((k*(k-1)+1)%M))%M;
				else ans=(ans*((k*k)%M))%M;
			}
			p=v[i].y;
//			cout<<v[i].y<<" ";
		}
		if(v[v.size()-1].y!=n)ans=(ans*ksm(k*k,n-v[v.size()-1].y,M))%M;
		cout<<ans<<endl;
	}
	return 0;
} 
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/
