#include<bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define db double
using namespace std;

const int MAXX=1e5+10;
const int P=1e9+7;

int T,n,m,V;
struct Data {
	int x,w;
	bool operator<(const Data &rhs) const {
		return x<rhs.x; 
	}
}a[MAXX];
bool check1() {
	for(int i=2;i<=m;i++) {
		if(a[i].x==a[i-1].x&&a[i].w!=a[i-1].w) {
			cout<<"0\n";
			return 0;
		}
	}
	return 1;
}
int Pow(int a,int b) {
	if(b<0) return 0;
	int res=1;
	while(b) {
		if(b&1) res=res*a%P;
		a=a*a%P;
		b>>=1;
	}
	return res%P;
}
void sol() {
	int ans=0;
	int f=0,g=1;
	if(a[1].x!=1) g=Pow(V,2*(a[1].x-1)-1);
	for(int i=1;i<=m;i++) {
		if(i!=m&&a[i].x==a[i+1].x) continue;
//		cout<<"check "<<i<<" "<<f<<" "<<g<<'\n';
		int num=(a[i+1].x-a[i].x-1);
		(ans+=f*(V-1)%P*Pow(V,2*(n-a[i].x))%P)%=P;
		int ft=0,gt=0,tmp=Pow(V,num),res=Pow(V,2*num+1),kt=((res-tmp)%P+P)%P;
		int op=((a[i].x!=1)?V:1);
		ft=(f*tmp%P+g*tmp%P*op%P)%P;
		gt=(f*kt%P+g*kt%P*op%P)%P;
		f=(ft%P+P)%P,g=(gt%P+P)%P;
	}
	int tot=Pow(V,2*(n-1));
//	cout<<"tot = "<<tot<<'\n';
	tot=((tot-ans)%P+P)%P;
	cout<<tot<<'\n';
}
signed main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>T;
	//int long long --
	while(T--) {
		cin>>n>>m>>V;
		for(int i=1;i<=m;i++) cin>>a[i].x>>a[i].w;
		sort(a+1,a+1+m);
		//check1
		if(!check1()) continue;
		//check v=1
		if(V==1) { cout<<"1\n";	continue;}
		//sol
		sol();
		//clear 
	}

	return 0;
}
