#include <bits/stdc++.h>
#define start_code signed main(){
#define stop_code return 0;}
#define openfile(x) (freopen(#x".in","r",stdin),freopen(#x".out","w",stdout))
#define elif else if
#define int long long
using namespace std;
const int MOD = 1e9+7;
int T,n,m,v,c,d,ans,las1,las2;
bool flag;
map<int,int> l;
unordered_map<int,bool> w;
int power(int x,int y)
{
	int cnt = 1;
	for (int i = 0;i < y;i ++)
		(cnt *= x) %= MOD;
	return cnt;
}
start_code
openfile(assign);
cin>>T;
while (T --)
{
	l.clear(),w.clear();
	las1 = las2 = -1,ans = 0,flag = false;
	cin>>n>>m>>v;
	for (int i = 0;i < m;i ++)
	{
		cin>>c>>d;
		if (l[c]) flag = true;
		l[c] = d;
	}
	if (flag)
	{
		cout<<0<<endl;
		continue ;
	}
	for (auto i:l)
	{
		if (las1 == -1) goto Loop;
		if (i.first == las1+1)
		{
			if (!w[las2]) w[las2] = 1,ans = (ans+v-1)%MOD;
			else ans = (ans+1)%MOD;
		}
		Loop: las1 = i.first,las2 = i.second;
	}
	cout<<(int)power((v*v%MOD+MOD-ans)%MOD,n-1)%MOD<<endl;
}
stop_code
