#include<bits/stdc++.h>
#define ll long long
#define ls u<<1
#define rs u<<1|1
#define up(i,a,b) for(int i=a;i<=b;i++)
#define dn(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=2e5+10,mod=1e9+7;
int _,n,m,v;
ll ywj[N],ans=1;
struct node
{
	ll x,v;	
}a[N];
ll ksm(ll a,ll k)
{
	ll ans=1;
	while(k)
	{
		if(k&1)(ans*=a)%=mod;
		(a*=a)%=mod;
		k>>=1;
	}
	return ans;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>_;
	while(_--)
	{
		cin>>n>>m>>v;
		bool ok=1;
		up(i,1,m) 
		{
			cin>>a[i].x>>a[i].v;
		}
		sort(a+1,a+m+1,[](node a,node b){return a.x<b.x;});
		up(i,1,m-1)
		{
			if(a[i].x==a[i+1].x&&a[i].v!=a[i+1].v)
			{
				ok=0;
				break;
			}
		}
		if(!ok)
		{
			cout<<0<<'\n';
			continue;
		}
		ans=1;
		up(i,1,m-1)
		{
			if(a[i].x==a[i+1].x)continue;
			ywj[i]=((ksm(v,2*(a[i+1].x-a[i].x))-(v-1)%mod*ksm(v,a[i+1].x-a[i].x-1))%mod+mod)%mod;
			ans=ans*ywj[i]%mod;
		}
		ans=ans*ksm(v,2*(a[1].x-1))%mod*ksm(v,2*(n-a[m].x))%mod;
		cout<<ans<<'\n';
	}
	return 0;
}
/*
1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2
*/
