#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = (a);i <= (b);i++)
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int n,m,v;
int c[N],d[N],x[N],a[N],b[N];
bool dfs(int cur,int xx)
{
	if (x[cur] != -1 && xx != x[cur])
		return false;
	if (cur == n)
		return true;
	if (xx == a[cur])
		return dfs(cur + 1,b[cur]);
	else
		return dfs(cur + 1,1) || dfs(cur + 1,2);
}
void solve1()
{
	//枚举a
	int lmt = (1 << (n - 1)) - 1;
	int ans = 0;
	rep1(i,1,n)
		x[i] = -1;
	rep1(i,1,m)
		x[c[i]] = d[i];
	rep1(i,0,lmt)
		rep1(j,0,lmt)
		{
			rep1(jj,0,n - 2)
				a[jj + 1] = ((i >> jj) & 1) + 1;
			rep1(jj,0,n - 2)
				b[jj + 1] = ((j >> jj) & 1) + 1;
			if (dfs(1,1) || dfs(1,2))
				ans++;
		}
	cout << ans << '\n';
}
int qpow(int x,int y)
{
	int ans = 1;
	while (y)
	{
		if (y & 1)
			ans = 1ll * ans * x % mod;
		y >>= 1;
		x = 1ll * x * x % mod;
	}
	return ans;
}
void solve2()
{
	//所有元素都是确定的
	//那么不能出现a=x,b=(!y)的限制 
	//每个禁了v-1种情况，还剩v^2-v+1
	cout << qpow((1ll * v * v - v + 1) % mod,n - 1) << '\n';
}
void solve()
{
	cin >> n >> m >> v;
	map<int,int> mp;
	rep1(i,1,m)
	{
		cin >> c[i] >> d[i];
		if (mp[c[i]] != 0 && mp[c[i]] != d[i])
		{
			cout << "0\n";
			return;
		}
		mp[c[i]] = d[i];
	}
	//特殊性质1：v<=2
	if (v <= 2 && n <= 12 && m <= 12)
		solve1();
	//特殊性质2：A
	else if (m == n)
		solve2();
}
int main()
{
 	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t;
	cin >> t;
	while (t--)
		solve();
}

