#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<vector>
#include<string>
#include<bitset>
#include<cstring>
#include<cstdlib>
#include<numeric>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e5+10;
const int N=1e5;
const int INF=0x3f3f3f3f;
const long long LINF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
int n,m,k,cnt=0,ans=0;
#define pii pair<int,int>
pii a[MAXN];
int p[MAXN];
const int B=65536;
const int MAXB=B+10;
int sp[MAXN],bp[MAXN];
inline int f(int x){
	return 1ll*bp[x>>16]*sp[x&(B-1)]%mod;
}
inline void init(){
	sp[0]=1;
	bp[0]=1;
	for(int i=1;i<=B;i++)
	{
		sp[i]=1ll*sp[i-1]*k%mod;
	}
	for(int i=1;i<=B;i++)
	{
		bp[i]=1ll*bp[i-1]*sp[B]%mod;
	}
}
inline void solve(){
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&a[i].first,&a[i].second);
	}
	sort(a+1,a+1+m);
	init();
	cnt=0;
	for(int i=1;i<=m;i++)
	{
		if(a[i].first^a[i-1].first){
			cnt++;
			p[cnt]=a[i].first;
		}
		else if(a[i].second^a[i-1].second){
			puts("0");
			return ;
		}
	}
	ans=f((p[1]-1)<<1);
	for(int i=2;i<=cnt;i++)
	{
		ans=1ll*ans*(f((p[i]-p[i-1])<<1)-f(p[i]-p[i-1]-1)*(k-1ll)%mod+mod)%mod;
	}
	ans=1ll*ans*f((n-p[cnt])<<1)%mod;
	printf("%d\n",ans);
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	#ifdef LOCAL
	atexit([](){fprintf(stderr,"%.3lfs\n",(double)clock()/CLOCKS_PER_SEC);});
	#endif
	int t;
	scanf("%d",&t);
	while(t--)
	{
		solve();
	}
	return 0;
}