#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+10;
const int maxm=1e5+10;
const int mod=1e9+7;

ll n,m,v,c,d;
ll x[maxn],f[maxn];
bool vis;

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	
	int T;cin>>T;
	while(T--){
		//cout<<1111<<T<<"\n";
		vis=true;
		cin>>n>>m>>v;
		if(n>100000000){
			cout<<0<<"\n";
			continue;
		}
		for(ll i=0;i<=n;i++){
			x[i]=-1;
			f[i]=0;
		} 
		for(ll i=1;i<=m;i++){
			cin>>c>>d;
			if(x[c]!=-1 && x[c]!=d) vis=false;
			else x[c]=d;
		}
		
		if(vis==false){
			cout<<0<<"\n";
			continue;
		}
		
		if(x[1]!=-1) f[1]=1;
		else f[1]=v;
		for(ll i=1;i<n;i++){
			ll res;
			if(x[i]==-1){
				if(x[i+1]!=-1){
					res=(v*(v-1)+mod)%mod;
				}
				if(x[i+1]==-1){
					res=(v*v+mod)%mod;
				}
			}
			if(x[i]!=-1){
				if(x[i+1]!=-1){
					res=(1+(v-1)*v+mod)%mod;	
				}
				if(x[i+1]==-1){
					res=(v+v*(v-1)+mod)%mod;
				}
			}
			f[i+1]=(f[i]*res+mod)%mod;
		}
		cout<<f[n]<<"\n";
	}
	
	return 0;
}
