#include<bits/stdc++.h>
#define LL long long
#define Fi first
#define Se second
using namespace std;
const LL Mod=1e9+7;
inline LL Ksm(LL x,LL y)
{
	LL A=1;
	while(y)
	{
		if(y&1)A=A*x%Mod;
		x=x*x%Mod,y>>=1;
	}
	return A;
}
LL IV;
LL n,m,V;
inline LL Cal(LL x)
{
	LL S=(Ksm(V,x+1)-V+Mod)%Mod;
	return S*IV%Mod;
}
inline LL F(LL x)
{
	if(x==0)return V*(V-1)%Mod+1;
	LL R=Cal(2*x+1),L=Cal(x+1);
	LL S=(R-L+Mod)%Mod;
	return ((V-1)*S%Mod+Ksm(V,x)*((V-1)*V%Mod+1)%Mod)%Mod; 
}
inline LL F2(LL x)
{
	if(x==0)return 1;
	LL R=Cal(2*x-1),L=Cal(x-1);
	LL S=(R-L+Mod)%Mod;
	return ((V-1)*S%Mod+Ksm(V,x))%Mod; 
}
inline void Rd(LL &x,int f=1)
{
	x=0;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while('0'<=c&&c<='9')x=10*x+c-'0',c=getchar();
	x=x*f;
}
inline void Work()
{
	Rd(n),Rd(m),Rd(V);
	IV=Ksm(V-1,Mod-2);
	map<LL,LL>Ma;	 
	LL Ans=1,x,y;
	for(int i=1;i<=m;i++)
	{
		Rd(x),Rd(y);
		if(Ma.count(x)&&Ma[x]!=y)Ans=0;
		Ma[x]=y;
	}
	LL L=0;
	for(auto i:Ma)
	{
		if(L==0)Ans=Ans*Ksm(V,2*(i.Fi-1))%Mod;
		else Ans=Ans*F(i.Fi-L-1)%Mod;
		L=i.Fi;
	} 
	Ans=Ans*F2(n-L)%Mod;
	printf("%lld\n",Ans);		

} 
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;scanf("%d",&T);
	while(T--)Work();	
}
