#include<bits/stdc++.h>
using namespace std;
#define int long long int
const int mod=1e9+7;
const int N=1e5+5;
int T,n,m,v,ans;
int c[N],d[N];
int a[N];
bool spA,spB,spC;
map<int,int>mp;
int pow(int a,int b,int mod)
{
	int s=1;
	while(b)
	{
		if(b%2)s=(s*a)%mod;
		a=(a*a)%mod;
		b/=2;
	}
	return s%mod;
}
struct node
{
	int a,b;
}t[N];
int tp[N];
void dfs(int p)
{
	if(p==n)
	{
		for(int i=1;i<=n;i++)
			tp[i]=a[i];
		for(int i=1;i<=n;i++)
			if(tp[i])
			{
				if(t[i-1].a==tp[i-1]&&t[i-1].b!=tp[i])
					return ;
				if(t[i].a==tp[i])
				{
					if(tp[i+1]&&tp[i+1]!=t[i].b)
						return ;
					tp[i+1]=t[i].b;
				}
			}
		ans++;
		return ;
	}
	for(int i=1;i<=v;i++)
		for(int j=1;j<=v;j++)
		{
			t[p]={i,j};
			dfs(p+1);
		}
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>v;
		mp.clear();
		spA=(n==m),spB=1,spC=0;
		memset(a,0,sizeof(a));
		for(int i=1,x,y;i<=m;i++)
		{
			cin>>c[i]>>d[i];
			if(i!=c[i])spA=0;
			if(d[i]!=1)spB=0;
			if(mp[c[i]]&&mp[c[i]]!=d[i])spC=1;
			mp[c[i]]=d[i];
		}
		if(spC)
		{
			cout<<0<<endl;
			continue;
		}
		if(spA)
		{
			cout<<pow((v-1)*v%mod+1,n-1,mod)<<endl;
			continue;
		}
		if(m==1)
		{
			cout<<(pow((v*v)%mod,n-1,mod))%mod<<endl;
			continue;
		}
		if(spB)
		{
			ans=1;
			cout<<"wobudaoa"<<endl;
			continue;
		}
		for(int i=1;i<=m;i++)
			a[c[i]]=d[i];
		ans=0;
		dfs(1);
		cout<<ans<<endl;
	}
	return 0;
}
//maybe 60pts,2=WenLa
