#include<bits/stdc++.h>
using namespace std;
int T;
int n,m,v;
int c,d;
int nums[100005];
long long MAXN=1e9+7;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	for(int z=0;z<T;z++){
		bool jdg=0;
		for(int i=1;i<=n;i++){
			nums[i]=0;
		}
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++){
			cin>>c>>d;
			if(nums[c]!=0&&jdg==0&&nums[c]!=d){
				cout<<0<<endl;
				jdg=1;
			}
			nums[c]=d;
		}
		if(jdg)continue;
		int near=0;
		for(int i=1;i<n;i++){
			if(nums[i]!=0&&nums[i+1]!=0){
				near++;
			}
		}
		cout<<long(pow(v*v,n-1-near)*pow(v*v-1,near))%MAXN<<endl;
	}
	return 0;
}
