#include<bits/stdc++.h>
#define ll long long
#define ci const int
#define fo(i,l,r) for(int i=(l);i<=(r);++i)
#define fu(i,l,r) for(int i=(l);i<(r);++i)
#define fd(i,l,r) for(int i=(l);i>=(r);--i)
using namespace std;
inline ll read(){ll u,f=1;char o;while((o=getchar())<48||o>57)if(o=='-')f=-1;u=(o^48);while((o=getchar())>=48&&o<=57)u=(u<<1)+(u<<3)+(o^48);return u*f;}
bool ST;
ci N=4e5+5,mod=1e9+7;
ll ksm(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
ll n,m,v;
struct node
{
	ll x,val;
}a[N];
bool cmp(node x,node y)
{
	return x.x<y.x;
}
ll solve(ll x)
{
	ll ans=(ksm(v,2*x)-ksm(v,x)+mod)%mod;
	ans=(ans+ksm(v,x-1))%mod;
	return ans;
}
bool ED;
int main()
{cerr<<(&ST-&ED)/1048576.0<<'\n';
//	freopen("assign3.in","r",stdin);
//	freopen("1.out","w",stdout);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T=read();
	while(T--)
	{
		n=read();m=read(),v=read();
		fo(i,1,m)
		{
			ll x=read(),val=read();
			a[i]={x,val};
		}
		sort(a+1,a+m+1,cmp);
		ll ans=ksm(v,2*(a[1].x-1));
		ans=ans*ksm(v,2*(n-a[m].x))%mod;
		fo(i,1,m-1)
		{
			if(a[i].x==a[i+1].x)
			{
				if(a[i].val!=a[i+1].val)ans=0;
			}
			else ans=ans*solve(a[i+1].x-a[i].x)%mod;
		}
		cout<<ans<<'\n';
	}
}
