#include <bits/stdc++.h>
using namespace std;

int t,n,m,v,number[1000001];
long long dp[1000001];
const int mod=1e9+7;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	for(int tt=1;tt<=t;tt++){
		cin>>n>>m>>v;
		bool flag=false;
		if(n>1000000){
			cout<<0<<"\n";
			continue;
		}
		memset(number,0,sizeof(number));
		for(int i=1;i<=m;i++){
			int c,d;
			cin>>c>>d;
			if(c<1000001&&number[c]!=0&&d!=number[c]){
				cout<<0<<"\n";
				flag=true;
				break;
			}else if(c<1000001){
				number[c]=d;
			}
		}
		if(flag==true){
			continue;
		}
		memset(dp,0,sizeof(dp));
		dp[1]=1;
		for(int i=2;i<=n;i++){
			if(number[i]!=0&&number[i-1]!=0){
				dp[i]=dp[i-1]*(v-1)*v+dp[i-1];
			}else if((number[i]!=0&&number[i-1]==0)||(number[i-1]!=0&&number[i]==0)){
				dp[i]=dp[i-1]*v*v;
			}else if(number[i]==0&&number[i-1]==0){
				dp[i]=dp[i-1]*v*v;
			}
			dp[i]%=mod;
		}
		cout<<dp[n]<<"\n";
	}
}
