#include <bits/stdc++.h>
using namespace std;
typedef long long ll; 
typedef __int128 i128;
const int N=1e6+5,mod=1e9+7;
ll f[N][2],v;
int n,m;
int x[N];
ll qp(i128 a,i128 b){
	i128 ans=1;
	for(;b;b>>=1,a=(a*a)%mod){
		if(b&1){
			ans=(ans*a)%mod;
		} 
	}
	return ans;
}
namespace slv1{
	bool in(){
		bool fl=0;
		memset(x,0,sizeof(x));
		cin>>n>>m>>v;
		for(int i=1;i<=m;++i){
			int c,d;
			cin>>c>>d;
			if(n>N) continue;
			if(x[c] && x[c]!=d) fl=1;
			x[c]=d;
		}
		if(n>N && m!=1) return 0;
		if(fl) return 0;
		return 1;
	} 
	void solve(){
		if(!in()){
			cout<<0<<'\n';
			return;
		}
		if(m==1){
			cout<<qp(v*v,n-1)<<'\n';
			return ;
		}
		memset(f,0,sizeof(f));
		if(x[1]) f[1][1]=1;
		else f[1][0]=1;
		for(int i=2;i<=n;++i){
			if(x[i]){
				f[i][1]=(((i128)1*f[i-1][0]*v%mod*v)%mod+((i128)1*f[i-1][1]*((v-1)*v+1)%mod)%mod)%mod;
			}
			else{
				f[i][0]=((i128)1*(f[i-1][0]*v%mod*v)%mod+((i128)1*f[i-1][1]*((v-1)*v)%mod)%mod)%mod;
				f[i][1]=((i128)1*f[i-1][1]*v)%mod;
			}
		}
		cout<<(f[n][1]+f[n][0])%mod<<'\n';
	} 
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin>>T;
	while(T--){
		slv1::solve();
	} 
	return 0;
}



