#include<bits/stdc++.h>
#define int long long
#define Mod 1000000007
using namespace std;
namespace IO{
	void read(int &x){
		short f=1;x=0;char ch=getchar();
		while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
		while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
		x*=f;
		return;
	}
	void out(int x){
		if(x<0) putchar('-'),x=-x;
		if(x>9) out(x/10);
		putchar(x%10+'0');
		return;
	}
	void endl(){
		putchar('\n');
		return;
	}
}
using IO::read;
using IO::out;
using IO::endl;
const int N=500005;
int T,n,m,v;
int a[N],b[N],c[N];
struct node{
	int x,y;
}t[N];
unordered_map<int,int>mp;
bool pd(){
	mp.clear();
	for(int i=1;i<=m;i++){
		if(mp[t[i].x]&&mp[t[i].x]!=t[i].y){
			return true;
		}
		mp[t[i].x]=t[i].y;
	}
	return false;
}
bool cmp(node a,node b){
	return a.x<b.x;
}
bool spA(){
	if(n==m){
		for(int i=1;i<=m;i++){
			c[t[i].x]=t[i].y;
		}
		for(int i=1;i<=n;i++) 
			if(c[i]==0) return false;
		return true;
	}
	return false;
}
void solveA(){
	int res=1,tmp=(v*v-v+1)%Mod; 
	for(int i=1;i<n;i++) res=res*tmp%Mod;
	out(res),endl();
	return;
}
int ksm(int a,int b){
	int res=1;
	while(b){
		if(b&1) res=res*a%Mod;
		a=a*a%Mod;
		b>>=1; 
	}
	return res;
}
int ans;
void dfs(int x){
	if(x==n){
		bool flag=true;
		for(int i=1;i<n;i++){
			if(c[i]&&c[i+1]&&c[i]==a[i]&&c[i+1]!=b[i]){
				flag=false;
				break;
			}
		}
		if(flag) ans++;
		return;
	}
	else{
		a[x]=1,b[x]=1;
		dfs(x+1);
		a[x]=1,b[x]=2;
		dfs(x+1);
		a[x]=2,b[x]=1;
		dfs(x+1);
		a[x]=2,b[x]=2;
		dfs(x+1);
	}
	return;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	read(T);
	while(T--){
		read(n),read(m),read(v);
		for(int i=1;i<=m;i++){
			read(t[i].x),read(t[i].y);
		}
		if(pd()){
			printf("0\n");
			continue;
		}
		if(spA()){
			solveA();
			continue;
		}
		else if(v<=2){
			ans=0;
			for(int i=1;i<=m;i++){
				c[t[i].x]=t[i].y;
			}
			dfs(1);
			out(ans),endl();
			for(int i=1;i<=n;i++) c[i]=0;
		}
		else{
			stable_sort(t+1,t+m+1,cmp);
			int res=ksm(v*v%Mod,n-1),inv=ksm(v*v%Mod,Mod-2);
			for(int i=1;i<=m;i++){
				if(i==1||t[i].x!=t[i-1].x){
					if(t[i].x-t[i-1].x==1){
						res=res*inv%Mod;
						res=res*(v*v%Mod-v+1)%Mod;
					}
				}
			}
			res=(res+Mod)%Mod;
			out(res),endl();
		}
	}
	return 0;
}
