#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define fre(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
#define int long long
const int N=1e5+10,mod=1e9+7;
int t,n,m,v,ans,b[N],qb;
map<int,int> a;
void read(int &x)
{
	x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
}
int ksm(int x,int y){return y?y-2?ksm(ksm(x,y>>1),2)*(y&1?x:1)%mod:x*x%mod:1;}
signed main()
{
	fre("assign");
	read(t);
	while(t--)
	{
		qb=0;
		a.clear();
		ans=1;
		bool f=0;
		read(n),read(m),read(v);
		while(m--)
		{
			int c,d;
			read(c),read(d);
			if(a[c]!=0&&a[c]!=d)f=1;
			else if(!a[c])a[c]=d,b[++qb]=c;
		}
		if(f)
		{
			puts("0");
			continue;
		}
		sort(b+1,b+1+qb);
		ans=ans*ksm(v,2*(b[1]-1))%mod;
		ans=ans*ksm(v,2*(n-b[qb]))%mod;
		for(int i=2;i<=qb;i++)ans=ans*((ksm(v,2*(b[i]-b[i-1]))-ksm(v,b[i]-b[i-1]-1)*(v-1)%mod+mod)%mod)%mod;
		printf("%lld\n",ans);
	}
}