#include<bits/stdc++.h>
#define int long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int mo=1e9+7;
struct _{
	int c,d; 
}a[100020];
int n,m,v,ans,s,mm;
bool f;
map<int,int> mp;
int fpow(int x,int y)
{
	if(y<0)return 1;
	int ss=1;
	while(y)
	{
		if(y%2)ss*=x,ss%=mo;
		x*=x;x%=mo;
		y/=2;
	}
	return ss;
}
bool cmp(_ x,_ y)
{
	return x.c<y.c;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin>>T;
	while(T--)
	{
		mp.clear();ans=1;f=0;s=0;
		cin>>n>>m>>v;
		fo(i,1,m)
		{
			scanf("%lld%lld",&a[i].c,&a[i].d);
			if(mp.count(a[i].c)&&mp[a[i].c]!=a[i].d)
			{
				cout<<0<<endl;f=1;
				break;
			}
			mp[a[i].c]=a[i].d;
		}
		if(f)continue;
//		if(m==1)
//		{
//			ans=fpow(v,(n*2-2));
//			cout<<ans<<endl;
//			continue;
//		}
		ans=ans*fpow(v,(n*2-2))%mo;
		sort(a+1,a+m+1,cmp);
		fo(i,2,m)
			if(a[i].c!=a[i-1].c)
			{
				ans-=fpow(v,2*(a[i-1].c-1+n-a[i].c))*fpow(v,(a[i].c-a[i-1].c-1))%mo*(v-1)%mo,ans+=mo,ans%=mo;
			}
		cout<<ans<<endl; 
	} 
	return 0;
}

