#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Mod=1e9+7;
map<int,int> mp;
int ksm(int x,int k)
{
	if(k==0)return 1;
	if(k==1)return x%Mod;
	int temp=ksm(x,k/2)%Mod;
	if(k%2==1)return ((temp*temp)%Mod)*x%Mod;
	return (temp*temp)%Mod;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		mp.clear();
		bool b=false;
		int n,m,v;
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++)
		{
			int x,y;
			cin>>x>>y;
			if(mp.find(x)!=mp.end()&&mp[x]!=y)b=true;
			mp[x]=y;
		}
		if(b)
		{
			cout<<0<<endl;
			continue;
		}
		int anss=(v*v)%Mod,anst,cnt=0,cntt=0,ansn;
		anst=(v*(v-1)%Mod+1)%Mod,ansn=(v*(v-1)%Mod)%Mod;
		map<int,int>::iterator it=mp.begin();
		int lx=it->first,ly=it->second;
		it++;
		for(it=mp.begin();it!=mp.end();it++)
		{
			int nx=it->first,ny=it->second;
			if(nx==lx+1)
			{
				if(ly<=v&&ny<=v)cnt++;
				if(ny>v&&ly<=v)cntt++;
			}
			ly=ny,lx=nx;
		}
		cout<<(ksm(anst,cnt)%Mod)*(ksm(anss,n-1-cnt-cntt)%Mod)*(ksm(ansn,cntt)%Mod)%Mod<<endl;
	}
	return 0;	
}
