#include<bits/stdc++.h>
#define LL long long
#define db double
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;

/*
段与段之间是独立的 
若前面给了一个限制，可能会给一段长度为t的造成连锁反应 
枚举断点，贡献是一段等比数列求和 
*/

const int N=1e5+10,MOD=1e9+7;

int n,m,v,ans,inv,mu;
struct node
{
	int s,t,len;
}a[N]; int tot;
vector <int> g;
unordered_map <int,int> mp;

void addm(int &x,int y){(x+=y<0? MOD+y:y)%=MOD;}

int ksm(int x,int y)
{
	int res=1;
	while(y)
	{
		if(y&1) res=1LL*res*x%MOD;
		x=1LL*x*x%MOD; y>>=1;
	}
	return res;
}

int getsum(int t)
{
	int x=ksm(inv,t+1);
	addm(x,-inv);
	x=1LL*x*mu%MOD;
//	cout<<"hhh"<<x<<"\n";
//	x=ksm(x,MOD-2);
	return x;
}

void mian()
{
	scanf("%d%d%d",&n,&m,&v);
	
	ans=1; g.clear(); mp.clear();
	for(int i=1; i<=m; i++)
	{
		int x,y; scanf("%d%d",&x,&y);
		g.push_back(x);
		if(mp.find(x)==mp.end()) mp[x]=y;
		else if(mp[x]!=y) ans=0;
	}
	
	if(!ans) return puts("0"), void();
	
	tot=0;
	sort(g.begin(),g.end());
	g.erase(unique(g.begin(),g.end()),g.end());
	int lst=-1;
	for(int v:g)
	{
		if(v!=lst+1) a[++tot]={v,v,1};
		else a[tot].t++, a[tot].len++;
		lst=v;
	}
	
	a[++tot]={n+1,n+1,1};
	int vv=1LL*v*v%MOD;
	ans=ksm(vv,a[1].s-1);
	inv=ksm(v,MOD-2); mu=ksm(inv-1,MOD-2);
	
//	cout<<a[1].s<<" "<<ans<<"\n";
	for(int i=1; i<tot; i++)
	{
		int tmp=1LL*v*(v-1)%MOD; addm(tmp,1);
		int cur=a[i+1].s-a[i].t;
		int x=1;
		if(i!=tot-1)
		{
			x=1LL*ksm(v,2*cur)*(v-1)%MOD;
			x=1LL*x*getsum(cur)%MOD;
//			cout<<"get"<<x<<" ";
			addm(x,ksm(v,cur-1));	
		}
		else x=1LL*x*ksm(vv,cur-1)%MOD;
//		cout<<x<<" "; 
		x=1LL*x*ksm(tmp,a[i].len-1)%MOD;
//		cout<<x<<"\n";
		ans=1LL*ans*x%MOD;
	}
	
	printf("%d\n",ans);
}

int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);

	int T; scanf("%d",&T);
	while(T--) mian();

	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
*/

