#include<bits/stdc++.h>
#define ll long long 
#define LL __int128
#define fi first
#define se second
#define int long long
using namespace std;

const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int n, m, v;
map<int, int> vis;
struct node{int x, y;}a[maxn];

struct matrix{
	int n, m, a[4][4];
	matrix(){n = m = 0; memset(a, 0, sizeof(a));}
	void init(){n = m = 0; memset(a, 0, sizeof(a));}
}base, ans;
void Add(int &x, int y){x = (x + y) % mod;}

matrix operator * (matrix x, matrix y){	
	matrix z; z.n = x.n; z.m = x.m;
	for(int i = 1; i <= x.n; i++){
		for(int j = 1; j <= x.m; j++){
			for(int k = 1; k <= y.m; k++){
				Add(z.a[i][k], x.a[i][j] * y.a[j][k] % mod);
			}
		}
	}
	return z;
}

matrix qpow(matrix x, int y){
	matrix sum = x; y--;
	while(y){
		if(y & 1) sum = sum * x;
		x = x * x;
		y >>= 1;
	}
	return sum;
}

void solve(){
	cin >> n >> m >> v;
	vis.clear();
	bool flag = 0;
	for(int i = 1; i <= m; i++){
		cin >> a[i].x >> a[i].y;
		if(!vis[a[i].x]) vis[a[i].x] = a[i].y;
		else if(vis[a[i].x] != a[i].y) flag = 1;
	}
	if(flag) return cout << "0\n", void();
	base.init(); ans.init();
	base.n = base.m = 2; 
	base.a[1][1] = v * v % mod;
	base.a[2][1] = v * (v - 1) % mod;
	base.a[2][2] = v;
	ans.n = 1; ans.m = 2;
	if(vis[1]) ans.a[1][2] = 1;
	else ans.a[1][1] = 1;
	sort(a + 1, a + m + 1, [](node x, node y){return x.x < y.x;});
	int j = 1;
	for(int i = 1; i <= m; i++){
		if(a[i].x - j - 1 < 0) continue;
		if(a[i].x - j - 1) ans = ans * qpow(base, a[i].x - j - 1);
		j = a[i].x;
		ans.a[1][1] = (ans.a[1][1] * v % mod * v % mod + ans.a[1][2] * v % mod * (v - 1)) % mod;
		ans.a[1][2] = (ans.a[1][2] + ans.a[1][1]) % mod;
		ans.a[1][1] = 0;
	}
	if(j < n) ans = ans * qpow(base, n - j);
	cout << (ans.a[1][1] + ans.a[1][2]) % mod << '\n';
}

signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false);
	cout.tie(0);
	cin.tie(0);

	int t; cin >> t; while(t--) solve();

	return 0;
}