#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
const long long mod=1e9+7;
long long t,a,b,c;
map<long long,long long>mp;
vector<int>d;
long long quickpow(long long n,long long k)
{
	long long ans=1ll;
	n%=mod;
	while(k!=0)
	{
		if(k&1)
		  ans=ans*n%mod;
		n=n*n%mod;
		k>>=1;
	}
	return ans;
}
int main()
{ 
//	if(system("fc assign2.ans assign.out")){}
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	for(int z=1;z<=t;z++)
	{
		d.clear();
		long long cnt=0;
		bool flag=false;
		cin>>a>>b>>c;
		mp.clear();
		for(int y=1;y<=b;y++)
		{
			long long j,k;
			cin>>j>>k;
			if(mp[j]!=k&&mp[j]!=0)
			  flag=true;
			if(mp[j]==0)
			{
				cnt+=(bool)mp[j-1]+(bool)mp[j+1];
				d.push_back(j);
			}
			mp[j]=k;
		}
		if(flag==true)
		{
			cout<<0<<endl;
			continue;
		}
		sort(d.begin(),d.end());
		cout<<quickpow(c*(c-1)+1,cnt)*quickpow(c*c,a-cnt-1)%mod<<endl;
	}
	return 0;
}
/*
256
0
4096
129600
4194304
503279958
748525720
892897109
174281099
418965370

6 2
2 5
4 3
*/
