#include <bits/stdc++.h>
using namespace std;
const int N = 22;
int x[N];
int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T;
	cin >> T;
	while(T--)
	{
		int n, m, v;
		long long ans = 1;
		cin >> n >> m >> v;
		if(m == 1)
		{
			cin >> m;
			cin >> m;
			for(int i = 0; i < 2 * n - 2; i++)
			{
				ans *= v;
				ans = ans % 1000000007;
			}
		}
		else
		{
			ans = 0;
			memset(x, 0, sizeof(x));
			bool b = 0;
			for(int i = 0; i < m; i++)
			{
				int c, d;
				cin >> c >> d;
				if(x[c] && x[c] != d)
					b = 1;
				x[c] = d;
			}
			if(b)
			{
				cout << 0 << endl;
				continue;
			}
			if(v == 1 || n == 1)
			{
				cout << 1 << endl;
				continue;
			}
			long long k = 1;
			for(int i = 0; i < 2 * n - 2; i++)
				k *= 2;
			for(long long i = 0; i < k; i++)
			{
				bool bo = 1;
				long long t = i, y[N];
				y[1] = x[1];
				for(int j = 1; j < n; j++)
				{
					y[j + 1] = x[j + 1];
					int a = t % 2 + 1;
					t /= 2;
					int b = t % 2 + 1;
					t /= 2;
					if(!y[j] || y[j] != a)
						continue;
					if(y[j + 1] && y[j + 1] != b)
					{
						bo = 0;
						break;
					}
					y[j + 1] = b;
				}
				if(bo)
					ans++;
				if(ans == 1000000007)
					ans = 0;
			}
		}
		cout << ans << endl;
	}
	return 0;
}
