#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define gc getchar
#define pb push_back
#define abs_(x) max(x,-(x))
#define all(x) x.begin(),x.end()
#define y1 __
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define dFor(i,a,b) for(int i=(a);i>=(b);i--)
#define tomin(x,y) x=min(x,y)
#define tomax(x,y) x=max(x,y)
const int N=1e5+5;
const int P=1e9+7;
int read()
{
	int x=0; int y=1; char c=gc();
	for(;!isdigit(c);c=gc()) if(c=='-') y=0;
	for(;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^48);
	return y?x:-x; 
}
ll pow_(ll x,int y)
{
	ll s=1;
	for(;y;y>>=1,x=x*x%P) if(y&1) s=s*x%P;
	return s;
}
int n,m,V;
struct _N
{
	int x,y;
	bool operator <(_N t)const
	{
		return x<t.x;
	}
}a[N];
int work()
{
	n=read(),m=read(),V=read();
	For(i,1,m) a[i]=(_N){read(),read()};
	sort(a+1,a+m+1);
	For(i,2,m) if(a[i].x==a[i-1].x&&a[i].y!=a[i-1].y) return 0;
	ll ans=1;
	ans=ans*pow_(V,a[1].x*2-2)%P;
	ans=ans*pow_(V,(n-a[m].x+1)*2-2)%P;
//	cout<<ans<<endl;
	For(i,2,m) if(a[i].x^a[i-1].x)
	{
		int len=a[i].x-a[i-1].x-1;
		ll s=pow_(V,len)*(V-1)%P;
		s=(pow_(V,len*2+2)-s+P)%P;
		ans=ans*s%P;
	}
	return ans;
}
int main()
{
//	freopen("assign3.in","r",stdin);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T=read();
	while(T--) printf("%d\n",work());
	return 0;
}



/*
划分区间，每个区间独立 
貌似可以简单算
 
9:13 ：看完题，写完 T2 
*/





