#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const int N = 1e5 + 5,mod = 1e9 + 7;
int t,n,m,v;
struct node{
	int c,d;
}a[N];
bool operator<(node a,node b){
	return a.c < b.c;
}
bool operator==(node a,node b){
	return !(a < b || b < a);
}
map<int,int> mp;
ll qpow(int a,int b){
	int ans = 1;
	while(b){
		if(b & 1)ans = 1ll * ans * a % mod;
		a = 1ll * a * a % mod,b >>= 1;
	}
	return ans;
}
int calc(int dis){
	return (qpow(1ll * v * v % mod,dis) - qpow(v,dis) + qpow(v,dis - 1) + mod) % mod;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin >> t;
	while(t--){
		cin >> n >> m >> v;
		mp.clear();
		int ans = 1;
		for(int i = 1;i <= m;i++){
			cin >> a[i].c >> a[i].d;
			if(mp.count(a[i].c) && mp[a[i].c] != a[i].d)ans = 0;
			mp[a[i].c] = a[i].d;
		}
		if(!ans){
			cout << ans << endl;
			continue;
		}
//		cout << qpow(v * 2 % mod,n - 1) << endl;
		sort(a + 1,a + m + 1);
		int len = unique(a + 1,a + m + 1) - a - 1;
		if(a[1].c != 1)ans = 1ll * ans * qpow(1ll * v * v % mod,a[1].c - 1) % mod;
		if(a[len].c != n)ans = 1ll * ans * qpow(1ll * v * v % mod,n - a[len].c) % mod;
		for(int i = 2;i <= len;i++)ans = 1ll * ans * calc(a[i].c - a[i - 1].c) % mod;
		cout << ans << endl;
	}
}
