#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
const int maxn=1e5+10;
int t,n,m,v;
map<int,int> mp;
struct node{
	int x,y;
}a[maxn];
bool cmp(node a,node b){
	return a.x<b.x;
}
int ksm(int a,int b){
	if (b<=0)return 1;
	int res=1;
	for (;b;b>>=1,a=a*a%mod){
		if (b&1)res=res*a%mod;
	}
	return res;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>t;
	while (t--){
		cin>>n>>m>>v;
		mp.clear();
		bool fl=1;
		int cnt=0;
		for (int i=1;i<=m;i++){
			cin>>a[i].x>>a[i].y;
			if (mp[a[i].x]!=0&&mp[a[i].x]!=a[i].y)fl=0;
			if (mp[a[i].x]==0)cnt++;
			mp[a[i].x]=a[i].y;
		}
		sort(a+1,a+m+1,cmp);
		if (!fl){
			cout<<"0\n";
			continue;
		}
		if (!m){
			cout<<ksm(v*v%mod,n-1)%mod<<'\n';
			continue;
		}
		int ans=ksm(v*v%mod,a[1].x-1)*ksm(v*v%mod,n-a[m].x)%mod;
		for (int i=1;i<m;i++){
			if (a[i].x==a[i+1].x)continue;
			if (a[i].x==a[i+1].x-1)ans=ans*((v-1)*v%mod+1)%mod;
			else ans=ans*(ksm(v*v%mod,a[i+1].x-a[i].x)-ksm(v,a[i+1].x-a[i].x-1)*(v-1)%mod)%mod;
		}
		cout<<(ans%mod+mod)%mod<<'\n';
	}
	return 0;
}
