#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+10;
const int mod=1e9+7;
int T,n,m,v,ans=1,last=0;
int c[N],d[N],x[N];
bool ck=1,s=1;
int qp(int u,int v)
{
	int res=1;
	while(v)
	{
		if(v&1) res=res*u%mod;
		u=u*u%mod;
		v>>=1;
	}
	return res;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
    cin>>T;
    while(T--)
    {
    	memset(x,0,sizeof(x));
    	s=1;ans=1;last=0;
    	cin>>n>>m>>v;
        for(int i=1;i<=m;i++)
        {
        	cin>>c[i]>>d[i];
        	if(x[c[i]] && x[c[i]]!=d[i])
        	{
        		s=0;
        		break;
			}
			x[c[i]]=d[i];
		}
		if(s==0) 
		{
			cout<<0<<endl;
			continue;
		}
		for(int i=1;i<=n-1;i++)
		{
			if(x[i] && x[i+1]) 
			{
				ans=(ans*((v-1)*v%mod+1)%mod)%mod;
			}
			else ans=(ans*v*v%mod)%mod;
		}
		cout<<ans<<endl;
	}
	fclose(stdin);fclose(stdout);
	return 0;
}
