#include<bits/stdc++.h>
#define ll long long 
using namespace std;
const int N=1e7+10;
int in()
{
	int w=0,f=1;
	char c=getchar();
	while(!isdigit(c))
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(isdigit(c))
	{
		w=w*10+c-'0';
		c=getchar();
	}
	return w*f;
}
void out(int w)
{
	if(w<0) 
	{
		putchar('-');
		out(-w);
		return;
	}
	if(w>=10) out(w/10);
	putchar(w%10+'0');
}
int T,n,m,v,c,d,t[N];
ll mod=1000000007;
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--)
	{
		memset(t,0,sizeof(t));
		n=in();m=in();v=in();
		int i;
		for(i=1;i<=m;i++) 
		{
			c=in();d=in();
			if(!t[c]) t[c]=d;
			else
			{
				out(0);
				putchar('\n');
				break;
			}
		}
		if(i!=m+1) continue;                 
		ll ans=(ll)v;
		out((ll)pow((ll)v*v%mod,n-1)%mod);
		putchar('\n');
	}
	return 0;
 } 
/*
3
2 1 2
1 1

2 2 2
1 1
2 2

2 2 2
1 1
1 2

*/
