#include <bits/stdc++.h>
using namespace std;

const long long mod = 1e9 + 7;
long long fpow(long long a, long long b, long long p){
	if(b == 0)
		return 1;
	long long t = fpow(a, b / 2, p);
	if(b % 2)
		return t * t % p * a % p;
	return t * t % p;
}
long long t;
long long n, m, v;
struct pnt{
	long long c, d;
	bool operator<(const pnt& x)const{
		return c < x.c;
	}
} p[100005];
long long a[100005], b[100005], cur = 0;
long long pr[100005] = {0};
void mian(){
	cur = 0;
	memset(pr, 0, sizeof(pr));
	cin >> n >> m >> v;
	for(int i = 1;i <= m;i++)
		cin >> p[i].c >> p[i].d;
	sort(p + 1, p + m + 1);
	for(int i = 2;i <= m;i++)
		if(p[i].c == p[i - 1].c && p[i].d != p[i - 1].d){
			cout << 0 << endl;
			return ;
		} 
	for(int i = 1;i <= m;i++)
		if(p[i].c != p[i - 1].c)
			cur++, a[cur] = p[i].c, b[cur] = p[i].d;
	long long ans = fpow(v, (n - 1 - a[cur] + a[1]) * 2, mod);
	for(int i = 2;i <= cur;i++){
		long long cnt = (v - 1) * fpow(v, a[i] - a[i - 1] - 1, mod) % mod;
		cnt = ((fpow(v, (a[i] - a[i - 1]) * 2, mod) - cnt) % mod + mod) % mod;
		ans = ans * cnt % mod;
	}
	cout << ans << endl;
	return ;
}

int main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	cin >> t;
	for(int T = 1;T <= t;T++)
		mian();
	return 0;
}
/*
1
10 5 2
2 2
3 2
4 2
7 2
10 2
*/
