#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mininf = 1e9 + 13;
#define pb emplace_back
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0' or ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}while(ch>='0' and ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();} return x * f;}
inline void write(int x){if(x<0){putchar('-');x=-x;}if(x>=10){write(x/10);}putchar(x%10+'0');}
#define put() putchar(' ')
#define endl puts("")
const int MAX = 1e5 + 10;
const int mod = 1e9 + 7;
inline int quickPower(int a, int b, int p){
	int ans = 1, base = a;
	while(b){
		if(b & 1)	ans = ans * base % p;
		base = base * base % p;
		b >>= 1;
	}
	return ans;
}

pair <int, int> a[MAX];	

void solve(){
	int n = read(), m = read(), V = read();
	int ans = quickPower(V * V % mod, n - 1, mod);
	for(int i = 1; i <= m; i++){
		a[i].first = read(), a[i].second = read();
	}
	sort(a + 1, a + m + 1);
	for(int i = 2; i <= m; i++){
		if(a[i].first == a[i - 1].first){
			if(a[i].second != a[i - 1].second){
				puts("0");
				return ;
			}
		}
	}
	int n2 = unique(a + 1, a + m + 1) - a - 1;
	if(n2 == 1)	write(ans), endl;
	else{
		ans = 1;
		for(int i = 1; i < n2; i++){
			if(i == 1)	ans = quickPower(V * V % mod, a[i].first - 1, mod);
			int ret = quickPower(V * V % mod, a[i + 1].first - a[i].first, mod);
			ret = (ret + mod - quickPower(V, a[i + 1].first - a[i].first - 1, mod) * (V - 1) % mod) % mod;
			ans = ans * ret % mod;
		}
		ans = ans * (quickPower(V * V % mod, n - a[n2].first, mod)) % mod;
		write(ans), endl;
	}
}

signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int t = read();
	while(t--)	solve();
	return 0;
}