#include<bits/stdc++.h>
using namespace std;
long long n,m,v;
map<long long,long long>x;
map<long long,long long>b;
long long c;
long long d;
long long dfs(int now){
	if(now==n)return 1;
	long long as=0;
	if(x[now]==0){
		as=v*v;
	}else{
		if(b[now]==0)as=v*v;
		else as=(v-1)*v+1;
	}
	if(as>0){
		long long op=dfs(now+1)%1000000007;
		return (as*op)%1000000007;
	}else return 0;
}
void sol(){
	x.clear();
	b.clear();
	cin>>n>>m>>v;
	for(int i=1;i<=m;i++){
		cin>>c>>d;
		if(x[c]==0||x[c]==d){
			x[c]=d;
			b[c-1]=x[c];
		}else{
			cout<<0<<endl;
			return;
		}
	}
	cout<<dfs(1)<<endl;
}
int main(){
	freopen("assign.in","r",stdin);freopen("assign.out","w",stdout);
	int t=1;
	cin>>t;
	for(int i=1;i<=t;i++){
		sol();
	}
}
/*
10 5 2
10 2
7 2
2 2
4 2
3 2

129600

4 4 4
3 3
 3 3 5 5

3 3
4 4 4
 4 4 4 4


*/
