#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = a; i <= b; i++)
const int mod = 1e9 + 7, N = 1e5 + 5;
inline int read() {
	int ans = 0, f = 1; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -f;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) ans = (ans << 3) + (ans << 1) + ch - '0';
	return f * ans;
}
int x[N], n, m, v;
long long ans;
int ind[N], p[16], a[N], b[N]; 
bool chec() {
//	memset(x, 0, sizeof(x));
	rep(i,1,n - 1) {
		if(ind[i] == a[i] && ind[i + 1] && ind[i + 1] != b[i]) return 0; 
		if(i <= n - 2) {
			if(ind[i] && ind[i + 2] && ind[i] == a[i] && ind[i + 2]
			 != b[i + 1] && ind[i] == ind[i + 2] && a[i + 1] == b[i]) return 0;
		}
	}
	return 1;
}
void dfs(int dep) {
	if(dep >= n) {
		ans += chec();
		return; 
	}  rep(i,1,2) {
		rep(j,1,2) {
			a[dep] = i; b[dep] = j;
			dfs(dep + 1);
		}
	}
}
int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T = read();
	while(T--) {
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		memset(ind, 0, sizeof(ind));
		n = read(), m = read(), v = read();
		ans = 0;
		bool flag = 0;
		rep(i,1,m) {
			int tmp = read(); int val = read();
			if(ind[tmp] && val != ind[tmp]) {
				flag = 1;
			}
			ind[tmp] = val;
		}
		if(flag == 1) {
			cout << 0 << endl;
			continue;
		}
		if(n <= 12) {
			if(v == 1) {
				cout << 1 << endl;
				continue;
			} else {
				dfs(1);	
				cout << ans % mod << endl;			
			}
		}	
	}
	
	return 0;
} 
