#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
int t,n,m,a[100005];
map<int,int> mp;
bool pd;
ll ans,v;
ll po(ll x,ll y){
	ll s=1;
	for(;y;y>>=1,x=x*x%mod)
	if(y&1)s=s*x%mod;
	return s;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>t;
	while(t--){
		cin>>n>>m>>v;
		mp.clear();ans=1;
		for(int i=1,x,y;i<=m;++i){
			cin>>x>>y;
			if(mp.find(x)==mp.end())mp[x]=y;
			else if(mp[x]!=y)ans=0;
		}
		if(!ans){
			cout<<0<<'\n';
			continue;
		}
		if(v==1){
			cout<<1<<'\n';
			continue;
		}
		m=0;
		for(auto i:mp)a[++m]=i.first;
		ans=po(v*v%mod,a[1]-1);
		for(int i=2;i<=m;++i)
			ans=ans*(po(v*v%mod,a[i]-a[i-1])-po(v,a[i]-a[i-1]-1)*(v-1)%mod+mod)%mod;
		ans=ans*po(v*v%mod,n-a[m])%mod;
		cout<<ans<<'\n';
	}
	return 0;
}
