#include <iostream>
#include <cstring>
#include <map>
#include <algorithm>
#define PII pair<int,int>

using namespace std;

const int N=1e5+5,mod=1e9+7;

int t,n,m,v,c[N],d[N],f[N][2]; // 1ÖÖ vÖÖ 
int t0[2][2],t1[2][2],tmp[2][2],pw[31][2][2],ans[2][2];
PII qwq[N];

void mul(int a[2][2],int b[2][2],int c[2][2]){// a = b * c
	tmp[0][0]=tmp[0][1]=tmp[1][0]=tmp[1][1]=0;
	for (int k=0;k<2;k++)
		for (int i=0;i<2;i++)
			for (int j=0;j<2;j++)
				tmp[i][j]=(tmp[i][j]+1ll*b[i][k]*c[k][j])%mod;
	a[0][0]=tmp[0][0],a[0][1]=tmp[0][1],
	a[1][0]=tmp[1][0],a[1][1]=tmp[1][1];
}

void qpow(int k){
	for (int i=30;~i;i--)
		if (k>>i&1) mul(ans,ans,pw[i]);
}

int main(){
	freopen("assign.in","r",stdin),freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0);
	cin>>t;
	while (t--){
		cin>>n>>m>>v;
		t0[0][0]=v,t0[0][1]=(v-1ll)*v%mod,t0[1][0]=0,t0[1][1]=1ll*v*v%mod;
		t1[0][0]=((v-1ll)*v+1)%mod,t1[0][1]=0,t1[1][0]=1ll*v*v%mod,t1[1][1]=0;
		memcpy(pw[0],t0,sizeof t0);
		for (int i=1;i<=30;i++) mul(pw[i],pw[i-1],pw[i-1]);
		bool ok=1;
		map<int,int> lst;
		for (int i=1;i<=m;i++){
			cin>>c[i]>>d[i];
			if (!lst.count(c[i])||lst[c[i]]==d[i])
				lst[c[i]]=d[i];
			else
				ok=0;
		}
		if (!ok){
			cout<<"0\n";
			continue;
		}
		ans[0][0]=ans[1][1]=1,ans[0][1]=ans[1][0]=0;
		for (int i=1;i<=m;i++) qwq[i]={c[i],d[i]};
		sort(qwq+1,qwq+m+1),m=unique(qwq+1,qwq+m+1)-qwq-1;
		for (int i=1,r=1;i<=m;i++)
			if (qwq[i].first>1)
				qpow(qwq[i].first-r-1),mul(ans,ans,t1),r=qwq[i].first;
		qpow(n-qwq[m].first);
		if (lst.count(1)) cout<<((ans[0][1]+ans[0][0])%mod+mod)%mod<<"\n";
		else cout<<((ans[1][1]+ans[1][0])%mod+mod)%mod<<"\n"; 
	}
	cout<<flush,fclose(stdin),fclose(stdout);	
	return 0;
}
