#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
const int MOD=1e9+7;
int T;
int n,m,v;
long long ans;
struct node{
	int c,d;
}q[MAXN];
map <int,int> a,b; 
bool cmp(node a,node b)
{
	return a.c<b.c;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		for(int i=1;i<=m;i++)
		{
			a[q[i].c]=0;
		}
		ans=1;
		scanf("%d %d %d",&n,&m,&v);
		bool check=false;
		for(int i=1;i<=m;i++)
		{
			scanf("%d %d",&q[i].c,&q[i].d);
			if(check) continue;
			if(a[q[i].c]!=0 && a[q[i].c]!=q[i].d) {check=true;continue;}
			a[q[i].c]=q[i].d;
		}
		if(check)
		{
			printf("0\n");
			continue;
		}
		if(n>1e6)
		{
			printf("A\n");
			continue;
		}
		for(int i=1;i<n;i++)
		{
			if(a[i+1]==0)
			{
				ans=(ans*v%MOD*v%MOD)%MOD;
			}else{
				if(a[i]!=0)
				{
					ans=(ans*(v-1)%MOD*v%MOD)%MOD;
					ans=(b[i]==1?ans*v:ans+1);
					b[i+1]=1;
				}else{
					ans=(ans*v%MOD*v%MOD)%MOD;
				}
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}  
