#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
ll mod = 1e9 + 7;
ll fm(ll a,ll b)
{
	ll ret = 1;
	while(b)
	{
		if(b&1)ret = ret*a%mod;
		a = a*a%mod;
		b>>=1;
	}
	return ret;
}
const int N = 1e5 + 10;
struct node{
	ll index;
	ll d;
}k[N];
bool cmp(node a,node b)
{
	return a.index<b.index;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	
	int T;
	scanf("%d",&T);
	while(T--)
	{
		ll n,m,v;
		scanf("%lld%lld%lld",&n,&m,&v);
		for(int i = 1;i<=m;i++)
			scanf("%lld%lld",&k[i].index,&k[i].d);
		if(m == 1)
		{
			cout<<fm(v,2*(n-1))<<endl;
			continue;
		}
		sort(k+1,k+m+1,cmp);
		bool flag = false;
		for(int i = 1;i<m;i++)
		{
			if(k[i].index == k[i+1].index&&k[i].d != k[i+1].d)
			{
				flag = true;
				break;
			}
		}
		if(flag)
		{
			cout<<0<<endl;
			continue;
		}
		if(m == n)
		{
			cout<<fm(v*v-v+1,n-1)<<endl;
			continue;
		}
		ll ans = fm(v*v,k[1].index-1);
		for(int i = 1;i<m;i++)
		{
			if(k[i].index==k[i+1].index)continue;
			ans = ans*(v*v-v+1)%mod*fm(v,2*(k[i+1].index-k[i].index-1))%mod;
		}
		ans = ans*fm(v*v,n-k[n].index)%mod;
		cout<<ans<<endl;
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
 } 
