#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5,mod=1e9+7;
int t,n,m,k,ans;
struct node
{
	int p,v;
	bool operator <(const node o)const
	{
		return p<o.p;
	}
}r[N];
int poww(int x,int p)
{
	int y=1;
	while(p)
	{
		if(p&1)y=y*x%mod;
		x=x*x%mod;
		p/=2; 
	}
	return y;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld%lld%lld",&n,&m,&k);
		for(int i=1;i<=m;i++)scanf("%lld%lld",&r[i].p,&r[i].v);
		sort(r+1,r+1+m);
		int len=1;
		ans=1;
		for(int i=1;i<m;i++)
		{
			if(r[i].p==r[i+1].p)
			{
				if(r[i].v!=r[i+1].v)
				{
					ans=0;
					break;
				}
				else continue;
			}
			ans=ans*((poww(k,(r[i+1].p-r[i].p)*2)-poww(k,r[i+1].p-r[i].p-1)*(k-1)%mod+mod)%mod)%mod;
		}
		if(m)len=r[1].p-1+n-r[m].p;
		else len=n-1;
		ans=ans*poww(k,len*2)%mod;
		printf("%lld\n",ans);
	}
}
