#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
#define pb push_back
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr);
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
namespace fastio{
	template<typename T> inline void read(T &t){
		T x = 0, f = 1;
		char c = getchar();
		while(!isdigit(c)){
			if(c == '-') f = -f;
			c = getchar();
		}
		while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
		t = x * f;
	}
	template<typename T, typename ... Args> inline void read(T &t, Args &...args){
		read(t), read(args...);
	}
}
using namespace fastio;
#define multitest() int T; read(T); for(int tCase = 1; tCase <= T; ++tCase)
//bool stt[2];
const ll mod = 1e9 + 7;
void add(ll &x, ll y){x = (x + y) % mod;}
void dec(ll &x, ll y){x = (x - y + mod) % mod;}
const int N = 1e5 + 5;
ll n, m, v, len, lsh[N], p[N];
pii a[N];
void clr(){
	_rep(i, 1, m) a[i].fi = a[i].se = 0;
	_rep(i, 1, len) p[i] = lsh[i] = 0;
	len = 0;
}
struct matrix{
	ll c[2][2];
	void clr(){c[0][0] = c[0][1] = c[1][0] = c[1][1] = 0;}
	void init(){c[0][0] = c[1][1] = 1, c[0][1] = c[1][0] = 0;}
	matrix operator*(const matrix b)const{
		matrix x; x.clr();
		_rep(i, 0, 1) _rep(j, 0, 1){
			_rep(k, 0, 1) x.c[i][j] += c[i][k] * b.c[k][j];
			x.c[i][j] %= mod;
		}
		return x;
	}
}G[2];
matrix ksm(matrix base, ll h){
	matrix res; res.init();
	while(h){
		if(h & 1ll) res = res * base;
		h >>= 1ll, base = base * base;
	}
	return res;
}
void build(){
	G[0].clr(), G[1].clr();
	G[0].c[0][0] = v * v % mod, G[0].c[0][1] = v * (v - 1) % mod, G[0].c[1][1] = v;
	G[1].c[0][0] = v * (v - 1) % mod, G[1].c[0][1] = (v - 1), G[1].c[1][0] = v, G[1].c[1][1] = 1;
}
void solve(){
	read(n, m, v);
	build();
	_rep(i, 1, m){
		read(a[i].fi, a[i].se);
		lsh[++len] = a[i].fi;
	}
	lsh[++len] = n - 1;
	sort(lsh + 1, lsh + 1 + len), len = unique(lsh + 1, lsh + 1 + len) - lsh - 1;
	bool ok = 1;
	_rep(i, 1, m){
		a[i].fi = lower_bound(lsh + 1, lsh + 1 + len, a[i].fi) - lsh;
		if(p[a[i].fi] && p[a[i].fi] != a[i].se) return puts("0"), void();
		p[a[i].fi] = a[i].se;
	}	
	matrix cur; cur.clr();
	if(lsh[1] == 1 && p[1]) cur.c[0][0] = v - 1, cur.c[1][0] = 1;
	else cur.c[0][0] = v;
	int pos = len, lst = 1;
	_rep(i, 1, len){
		if(lsh[i] == 1) continue;
		if(lsh[i] == n){
			pos = len - 1;
			break;
		}
		cur = ksm(G[0], lsh[i] - lst - 1) * cur;
		if(p[i]) cur = G[1] * cur;
		else cur = G[0] * cur;
		lst = lsh[i];
	}
	ll ans = 0;
	if(pos == len - 1){
		add(ans, cur.c[0][0] * v % mod + cur.c[1][0]);
	}else{
		add(ans, cur.c[0][0] * v % mod + cur.c[1][0] * v % mod);
	}
	printf("%lld\n", ans);
}
//bool edd[2];
int main(){
//	debug("use %.3lfMB\n", abs(stt - edd) / 1024.00 / 1024.00);
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	multitest(){
		solve();
		clr();
	}
	return 0;
}
/*
1
5 1 2
1 2

*/
/*
3
2 1 2
1 1

2 2 2
1 1
2 2

2 2 2
1 1
1 2

4
3
0

*/
