#include <bits/stdc++.h>
#define I64 long long

using namespace std;

/*
我们实际上统计的是所有不合法的情况
也就是说，我们只需要证明，我们留下来的选项一定合法，就可以做到不重复，不遗漏
构造不难，由于 v >= 2，我们让所有没有确定位置的元素全部填与 a[i] 不同的数字即可 
*/

/*
注意：
1. 清空
2. 注意答案为 0 的情况
3. 注意一下 corner cases 
*/ 

struct integer {
	static const int P = (int) 1E9 + 7;
	int u;
	explicit operator int () const {
		return u;
	}
	integer() : u(0) {
		
	}
	integer(I64 x) : u((int) (x % P)) {
		if (u < 0) {
			u = u + P;
		}
	}
	integer& operator += (integer a) {
		u = u + a.u;
		if (u >= P) {
			u = u - P;
		}
		return *this;
	}
	integer& operator -= (integer a) {
		u = u - a.u;
		if (u < 0) {
			u = u + P;
		}
		return *this;
	}
	integer& operator *= (integer a) {
		u = (int) ((I64) u * a.u % P);
		return *this;
	}
	friend integer operator + (integer a, integer b) {
		return a += b;
	}
	friend integer operator - (integer a, integer b) {
		return a -= b;
	}
	friend integer operator * (integer a, integer b) {
		return a *= b;
	}
	friend istream& operator >> (istream& i, integer &a) {
		I64 x;
		i >> x;
		a = integer(x);
		return i;
	}
	friend ostream& operator << (ostream& o, integer a) {
		o << (int) a;
		return o;
	}
};

integer power(integer a, I64 p) {
	integer x = 1;
	while (p > 0) {
		if (p % 2 == 1) {
			x = x * a;
		}
		a = a * a;
		p = p / 2;
	}
	return x;
} 

int n, m, k, c;
map<int, int> e;

bool o;
vector<int> a;

void clean() {
	e = map<int, int>();
	o = 1;
	a = vector<int>(0);
}

void solve() {
	cin >> n >> m >> k;
	clean();
	for (int i = 0; i < m; i++) {
		int p, u;
		cin >> p >> u;
		p = p - 1;
		if (e[p] != 0 && u != e[p]) {
			o = 0;
		}
		e[p] = u;
		a.push_back(p);
	}
	if (o == 0) {
		cout << 0 << "\n";
		return;
	}
	
	sort(a.begin(), a.end());
	a.erase(unique(a.begin(), a.end()), a.end());
	
	integer A = (integer) k * k;
	integer x = power(A, a[0]);
	c = (int) a.size();
	for (int i = 0, j = 0; i < c; i = j) {
		while (j < c && j - i == a[j] - a[i]) {
			j = j + 1;
		}
		int l = a[i];
		int r = (j < c ? a[j] - 1 : n - 1);
		int t = j - i, s = (r - l + 1) - t;
		// t 是 1 的个数
		// s 是 0 的个数 
		if (l == n - 1) {
			continue;
			// 如果左端点就是 n - 1，没有答案 
		}
		x = x * power(A - k + 1, t - 1);
		if (r < n - 1) {
			x = x * (power(A, s + 1) - (power((integer) k, s + 1) - power((integer) k, s)));
		} else {
			if (e[r] == 0) {
				x = x * power(A, s);
			} else {
				
			}
		}
//		cout << l << " " << r << " " << t << " " << s << "\n";
	}
	cout << x << "\n";
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		solve();
	}
	return 0;
}
