#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
#define MAXM 100005
#define int long long
#define MOD 1000000007
int t, n, m, v;
int c[MAXM], d[MAXM];
int qupow(int a, int b){
	int res = 1;
	while(b){
		if(b & 1)
			(res *= a) %= MOD;
		(a *= a) %= MOD;
		b >>= 1;
	}
	return res;
}
signed main(void){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout); 
	scanf("%lld", &t);
	
	while(t--){
		map<int, int> mp;
		memset(c, 0, sizeof(c));
		memset(d, 0, sizeof(d));
		scanf("%lld%lld%lld", &n, &m, &v);
		bool flag114 = 0, flagA = 1;
		for(int i = 1; i <= m; i++){
			scanf("%lld%lld", c + i, d + i);
			if(c[i] != i)
				flagA = 0;
			if(mp[c[i]] && mp[c[i]] != d[i]){
				printf("0\n");
				flag114 = 1;
				break;
			}
			else if(mp[c[i]]){
				i--;
				m--;
				continue;
			}
			mp[c[i]] = d[i];
		}
		if(flag114)
			continue;
		if(m == n && flagA){
			printf("%lld\n", qupow(v * (v - 1) + 1, n - 1) % MOD);
			continue;
		}
		else if(m == 1){
			printf("%lld\n", (qupow(v * v, n - 1)+MOD)%MOD);
		}
		else{
			sort(c + 1, c + m + 1);
			int ans = qupow(v * v, c[1] - 1);
			if(c[1] == 1)
				ans = 0;
			//printf("%lld\n\n", ans);
			for(int i = 2; i <= m; i++){
				ans = ((ans * (qupow(v * v, c[i] - c[i - 1]) - qupow(v, c[i] - c[i - 1] - 1) * (v - 1)) % MOD+MOD) % MOD + MOD) % MOD;
				//printf("%lld\n\n", ans);
			}
			ans = ((ans * qupow(v * v, n - c[m])) % MOD + MOD) % MOD;
			printf("%lld\n", ((ans % MOD) + MOD % MOD));
		}
	}
	return 0;
}

/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2

1
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2

1
12 2 2
7 2
7 2
*/
