#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;

const int mod=1e9+7;
struct eq
{
	int c,d;
	const bool operator<(const eq &y) const
	{
		return c<y.c;
	}
}a[100005];

long long power(long long a,long long b)
{
	long long ans=a,k=2,tmp=a;
	if(b==0)return 1;
	while(b>1)
	{
		while(b%k==0)
		{
			for(int i=1;i<k;i++)
			{
				ans*=tmp;
				ans%=mod;
			}
			tmp=ans;
			b/=k;
		}
		k++;
	}
	return ans;
}

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	cin >> T;
	while(T--)
	{
		long long n,m,v,ans=1;
		cin >> n >> m >> v;
		for(int i=1;i<=m;i++)
		{
			cin >> a[i].c >> a[i].d;
		}
		sort(a+1,a+m+1);
		for(int i=1;i<m;i++)
		{
			if(a[i].c==a[i+1].c&&a[i].d!=a[i+1].d)
			{
				ans=0;
				break;
			}
			else if(a[i].c!=a[i+1].c)
			{
				ans*=(power(v,2*(a[i+1].c-a[i].c))-power(v,a[i+1].c-a[i].c-1)*(v-1))%mod;
				n-=a[i+1].c-a[i].c;
			}
			ans%=mod;
		}
		ans*=power(v,(n-1)*2);
		ans%=mod;
		cout << ans << endl;
	}
	return 0;
}
