#include<iostream>
#include<cstdio>
#include<map> 
using namespace std;
const int N=1e6+10,mod=1e9+7;
int c[N],d[N];
map<int,int> a;
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;scanf("%d",&T);
	while(T--)
	{
		int n,m,v;
		scanf("%d%d%d",&n,&m,&v);
		bool if_=true; 
		for(int i=1;i<=m;i++)
		{
			scanf("%d %d",&c[i],&d[i]);
			if(a[c[i]]!=0) if_=false;
			a[c[i]]=d[i];
		}
		int ans=1;
		if(!if_) printf("0\n");
		else
		{
			for(int i=0;i<=n;i++)
			{
				if(!a[i]) ans*=v;
				ans%=mod;
			}
			printf("%d\n",ans+m-1);
		}
		for(int i=1;i<=m;i++) a[c[i]]=0;
	}
}
