#include <iostream>
#include <algorithm>
#include <unordered_map>
#define maxn 100001
#define int long long
using namespace std;
const int mod=1000000007;
int t,n,m,v,cnt,ans;
struct node{
	int id,val;
}d[maxn];
unordered_map<int,int>umap;
int qpow(int n,int m){
	if(m==0) return 0;
	int ans=1;
	while(m){
		if(m%2) ans=ans*n%mod;
		n=n*n%mod;
		m/=2;
	}
	return ans;
}
bool cmp(node a,node b){
	return a.id<b.id;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int i,j,k;
	bool flag;
	d[0].id=-1;
	cin>>t;
	while(t--){
		flag=0;
		ans=cnt=0;
		umap.clear();
		cin>>n>>m>>v;
		for(i=1;i<=m;i++){
			cin>>j>>k;
			if(flag) continue;
			if(umap[j]!=0){
				if(umap[j]==k) continue;
				else flag=1;
			}
			else{
				cnt++;
				umap[j]=k;
				d[cnt].id=j,d[cnt].val=k;
			}
		}
		if(flag){
			cout<<"0\n";
			continue;
		}
		m=cnt;
		sort(d+1,d+m+1,cmp);
		j=k=0;
		for(i=1;i<=m;i++){
			if(d[i].id==d[i-1].id+1) j++;
		}
		m=j;
		n=n-1-j;
		ans=qpow(v*v%mod,n)+qpow((v-1)*v%mod+1,m);
		cout<<ans%mod<<"\n";
	}
	fclose(stdin);
	fclose(stdout);
}

