#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;

const int N = 1e7 + 10;

int shu[N], ans;
int n, m, mx;

int qpow(int a, int k)
{
	int ans = 1;
	while(k)
	{
		if(k & 1)
			ans = (1ll * ans * a) % mod;
		a = (1ll * a * a) % mod;
		k >>= 1;
	}
	return ans;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
		
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
//	freopen("assign2.in","r",stdin);
	
	int T;
	cin >> T;
	while(T--)
	{
		bool flag = 0;
		cin >> n >> m >> mx;
		if(n > 12)
		{
			cout << "0\n";
			continue;
		}
		map<int,int>mp;
		int c, d;
		for(int i = 1; i <= m; ++i)
		{
			cin >> c >> d;
			if(mp[c] && mp[c]!=d)
				flag = 1;
			mp[c] = d;
		}
		if(flag)
		{
			cout << "0\n";
			continue;
		}
		if(n <= 12)
			cout << qpow(4, (n - 1)) << "\n";
	}
	return 0;
}
