#include<bits/stdc++.h>
#define ll long long
#define pb emplace_back
#define fi first
#define N 100005
using namespace std;
const int mod=1e9+7;
int n,m;
ll v;
map<int,int>tag;
struct mat{
	ll r[2][2];
}A,B,I,res;
mat operator *(mat a,mat b)
{
	mat c;
	memset(c.r,0,sizeof c.r);
	for(int i=0;i<2;i++)
		for(int k=0;k<2;k++)
			for(int j=0;j<2;j++)
				c.r[i][j]=(c.r[i][j]+a.r[i][k]*b.r[k][j])%mod;
	return c;
}
mat Qpow(mat a,ll x)
{
	mat sum=I;
	while(x)
	{
		if(x&1) sum=sum*a;
		a=a*a;
		x/=2;
	}
	return sum;
}
ll Solve()
{
	tag.clear();
	scanf("%d%d%lld",&n,&m,&v);
	A=(mat){{v*v%mod,0,(v*v-v)%mod,v}};
	B=(mat){{0,v*v%mod,0,(v*v-v+1)%mod}};
	for(int i=1;i<=m;++i)
	{
		int x,v;
		scanf("%d%d",&x,&v);
		if(!tag[x]) tag[x]=v;
		else if(tag[x]^v) return 0;
	}
	memset(res.r,0,sizeof res.r);
	if(tag[1]) res.r[0][1]=1;
	else res.r[0][0]=1;
	int last=1;
	for(pair<int,int> x:tag)
	{
		if(x.fi==1) continue;
		res=res*Qpow(A,x.fi-last-1)*B;
		last=x.fi;
	}
	res=res*Qpow(A,n-last);
	return (res.r[0][0]+res.r[0][1])%mod;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	scanf("%d",&T);
	I.r[0][0]=I.r[1][1]=1;
	while(T--) printf("%lld\n",Solve());
	return 0;
}
