#include<bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PII pair<int,int>
#define fi first
#define se second
#define VI vector<int>
#define PB emplace_back
#define PPB pop_back
#define ALL(x) x.begin(),x.end()
#define endl '\n'
using namespace std;

const int MOD=1e9+7;
int n,m,v;
vector<PII>lim;
int pw(int x,int y=MOD-2)
{
	int r=1;
	while(y)
	{
		if(y&1)r=1ll*r*x%MOD;
		x=1ll*x*x%MOD,y>>=1;
	}
	return r;
}

int main()
{
	freopen("assign.in","r",stdin),freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0);
	
	int _;cin>>_;while(_--)
{
	cin>>n>>m>>v;
	for(int i=1,x,y;i<=m;i++)cin>>x>>y,lim.PB(x,y);
	sort(ALL(lim));
	int ans=pw(v,(lim[0].fi-1+n-lim.back().fi)<<1);
	for(int i=1;i<lim.size();i++)
	{
		int k=lim[i].fi-lim[i-1].fi;
		if(!k){if(lim[i].se!=lim[i-1].se)ans=0;continue;}
		ans=1ll*ans*(pw(v,k<<1)-(v-1ll)*pw(v,k-1)%MOD+MOD)%MOD;
	}
	cout<<ans<<endl;
	lim.clear();
}
	
	return 0;
}
