#include<bits/stdc++.h>
#define ll long long
using namespace std;

const ll MOD = 1e9 + 7;
const int N = 1e6 + 10;
int T, n, m;
ll v;

ll exp(ll a, ll b){
	ll cnt = 1;
	while(b){
		if(b & 1)
			(cnt *= a) %= MOD;
		(a *= a) %= MOD, b >>= 1;
	}
	return cnt;
}

void solve(){
	map<int, ll> mp;
	bool f = 1;
	scanf("%d%d%lld", &n, &m, &v);
	for(int i=1,c,d;i<=m;i++){
		scanf("%d%d", &c, &d);
		if(mp[c] != 0 && mp[c] != d)
			f = 0;
		mp[c] = d;
	}
	if(!f)
		return printf("0\n"), void();
	ll ans = 1, sqr = v * v % MOD; int lst = 0;
	for(map<int, ll>::iterator i=mp.begin();i!=mp.end();i++){
		if(!lst)
			(ans *= exp(sqr, i->first - 1)) %= MOD;
		else{
			ll invalid = exp(v, i->first - lst - 1) * (v - 1ll) % MOD;
			(ans *= (exp(sqr, i->first - lst) - invalid) % MOD + MOD) %= MOD;
		}
		lst = i->first;
	}
	(ans *= exp(sqr, n - lst)) %= MOD;
	printf("%lld\n", ans);
}

int main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%d", &T);
	while(T--)
		solve();
	return 0;
}
