#include <bits/stdc++.h>
using namespace std;
int t, n, m, lim, ans;
struct node{
	int x, y;
	bool operator < (const node &A) const{
		return x < A.x;
	}
}a[100010];
const int mo = 1e9 + 7;
int power(int x, int y){
	int ret = 1;
	while (y){
		if (y&1) ret = 1LL * ret * x % mo;
		x = 1LL * x * x % mo;
		y >>= 1;
	}
	return ret;
}
map <int, int> mp;
map <int, int> :: iterator it;
int main(){
	freopen ("assign.in", "r", stdin);
	freopen ("assign.out", "w", stdout);
	scanf ("%d", &t);
	while (t --){
		ans = 1;
		mp.clear();
		scanf ("%d%d%d", &n, &m, &lim);
		for (int i=1; i<=m; i++){
			scanf ("%d%d", &a[i].x, &a[i].y);
			if (mp.find(a[i].x) != mp.end()){
				if (mp[a[i].x] != a[i].y) ans = 0;
			}
			mp[a[i].x] = a[i].y;
		}
		int lst = 0;
		for (it=mp.begin(); it!=mp.end(); it++){
			int sx = it->first, sy = it->second;
			if (lst){
				if (lst == sx-1) ans = 1LL*ans*(1LL*(lim-1)*lim%mo+1)%mo;
				else{
					int now = power(1LL*lim*lim%mo, sx-lst);
					int del = 1LL*power(lim, sx-lst-1)*(lim-1)%mo;
					now = (now - del + mo) % mo;
					ans = 1LL * ans * now % mo;
				}
			}
			else ans = 1LL*ans*power(1LL*lim*lim%mo, sx-1)%mo;
			lst = sx;
		}
		ans = 1LL*ans*power(1LL*lim*lim%mo, n-lst)%mo;
		printf ("%d\n", ans);
	}
	return 0;
}
