#include <bits/stdc++.h>
#define int long long
using namespace std;
int read(){
	int s = 0, f = 1;char ch = getchar();
	while(!isdigit(ch)){if(ch == '-')f = -1;ch = getchar();}
	while(isdigit(ch)){s = s * 10 + ch - '0';ch = getchar();}
	return s * f;
}
const int MAXN = 1e5 + 5, MOD = 1e9 + 7; 
struct node{
	int x, y;
	bool operator < (const node &zzz)const{
		return x < zzz.x;
	}
}a[MAXN];
int t, n, m, k;
int fpow(int a, int b){
	int res = 1;
	while(b){
		if(b & 1)res = res * a % MOD;
		a = a * a % MOD;
		b >>= 1ll;
	}
	return res;
}
unordered_map <int, int> mp;
signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	t = read();
	while(t --){
		n = read(), m = read(), k = read();
		mp.clear();
		for(int i = 1;i <= m;i ++)a[i] = {read(), read()};
		sort(a + 1, a + m + 1);
		if(m == 1){
			printf("%lld\n", fpow(k, 2 * n - 2));
			continue;
		}
		int ans = 1, flag = 0;
		for(int i = 1;i <= m;i ++){
			if(mp[a[i].x] && mp[a[i].x] != a[i].y)flag = 1;
			mp[a[i].x] = a[i].y;
		}
		if(flag){
			puts("0");
			continue;
		}
		for(int i = 2;i <= m;i ++){
			if(a[i].x == a[i - 1].x)continue;
			ans = ans * (fpow(k, 2 * (a[i].x - a[i - 1].x)) - fpow(k, (a[i].x - a[i - 1].x - 1)) * (k - 1) % MOD + MOD) % MOD;
		}
		ans = ans * fpow(k, (a[1].x - 1) * 2) % MOD;
		ans = ans * fpow(k, (n - a[m].x) * 2) % MOD;
		printf("%lld\n", ans);
	}
	return 0;
}

