#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
const int maxm=1e5+10;
struct node{
	int c,d;
}t[maxm];
inline bool cmp(node a,node b){
	return a.c<b.c;
}
inline int ksm(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans%mod;
}
struct Matrix {
	int a[2][2];
	Matrix(){
		memset(a,0,sizeof(a));
	}
};
Matrix mul(Matrix a,Matrix b){
	Matrix c;
	
	for(int i=0;i<=1;i++)
		for(int j=0;j<=1;j++)
			for(int k=0;k<=1;k++)
				c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j]%mod)%mod;
	return c;
}
Matrix qmul(Matrix a,int b){
	Matrix ans;
	ans.a[0][0]=ans.a[1][1]=1;
	while(b){
		if(b&1)ans=mul(ans,a);
		a=mul(a,a);
		b>>=1;
	}
	return ans;
}
inline int cal(int v,int j){
	Matrix C;
	C.a[0][1]=1;
	Matrix T;
	Matrix tmp;
	T.a[0][0]=T.a[1][0]=v;
	T.a[1][1]=1;
	int p1,p2;
	tmp=mul(C,qmul(T,2*j));
	p1=tmp.a[0][0];
	tmp=mul(C,qmul(T,j-1));
	p2=tmp.a[0][0];
	return ((p1-p2)%mod+mod)%mod;
}
inline int f(int v,int i){
	if(i==0)return ((v-1)*v%mod+1)%mod;
	return (cal(v,i)*v%mod*(v-1)%mod+ksm(v,i))%mod;
}
map<int,int>w;
inline void work(){
	int n,m,v;
	cin>>n>>m>>v;
	w.clear();
	sort(t+1,t+m+1,cmp);
	int now=1;
	for(int i=1;i<=m;i++){
		int c,d;
		cin>>c>>d;
		if(w[c]!=0){
			if(w[c]!=d){
				cout<<0<<"\n";
				return;
			}
		}
		w[c]=d;
	}
	now=0;
	for(auto l=w.begin();l!=w.end();l++){
		t[++now].c=l->first;
		t[now].d=l->second;
	}
	m=w.size();
	int ans=1;
	sort(t+1,t+m+1,cmp);
	ans=ksm(v,2*(t[1].c-1));
	for(int i=2;i<=m;i++){
		ans=ans*f(v,t[i].c-t[i-1].c-1)%mod;
	}
	ans=ans*ksm(v,2*(n-t[m].c))%mod;
	cout<<ans<<"\n";
	return;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin>>T;
	while(T--){
		work();
	}
	return 0;
}
/*
1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/
