#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
int T , n , m , v;
map<int , int> mp;
int vec[200005];
struct node{
	int l , r , summ;
} rp[200005];
int qpow(int a , int b){
	int res = 1 , aa = a;
	while(b){
		if(b & 1) res = (res * aa) % mod;
		aa = (aa * aa) % mod;
		b >>= 1; 
	}
	return res;
}
signed main(){
	freopen("assign.in" , "r" , stdin);
	freopen("assign.out" , "w" , stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	while(T--){
		cin >> n >> m >> v;
		mp.clear();
		bool flag = true;
		int cntt = 0;
		for(int i = 1 ; i <= m ; i++){
			int ci , di;
			cin >> ci >> di;
			if(mp[ci] == 0){
				mp[ci] = di;
				vec[++cntt] = ci;
			}
			else if(mp[ci] != di) flag = false; 
		}
		if(flag == false){
			cout << "0\n";
			continue;
		}
		sort(vec + 1 , vec + 1 + cntt);
		int cnt = 0;
		for(int i = 1 ; i <= cntt ; i++){
			int j = i;
			while(j + 1 <= cntt && vec[j + 1] - (j - i + 1) == vec[i]) j++;
			rp[++cnt].l = vec[i];
			rp[cnt].r = vec[j];
			i = j;
		}
		int anss = 1;
		for(int i = 1 ; i <= cnt ; i++){
			rp[i].summ = qpow((v * v % mod - v + 1 + mod) % mod , rp[i].r - rp[i].l); 
			anss = (anss * rp[i].summ) % mod;
		} 
		for(int i = 2 ; i <= cnt ; i++){
			int L = rp[i - 1].r , R = rp[i].l - 1;
			int LL = R - L + 1;
			anss = (anss * ((qpow(v , 2 * LL) - (qpow(v , LL) - qpow(v , LL - 1) + mod) % mod + mod) % mod)) % mod;
		}
		int ooo = n - rp[cnt].r;
		anss = anss * qpow(v * v % mod , ooo) % mod;
		anss = anss * qpow(v * v % mod , rp[1].l - 1) % mod;
		cout << anss << "\n";
	}
	return 0;
}

