#include <bits/stdc++.h>
#define MAXN 1000
#define mod 1000000007
using namespace std;
int x[MAXN];
int a[2] = {1,2};
int b[2] = {1,2};
int T,n,m,v,c,d,wa;
long long ans;
int main(){
	 freopen("assign.in","r",stdin);
	 freopen("assign.out","w",stdout);
	
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	cin >> T;
	for (int i = 1; i <= T; i++){
		cin >> n >> m >> v; 
		for (int j = 1; j <= m; j++){
			cin >> c >> d;
			if (x[c] == d || x[c] == 0){
				x[c] = d;
			}
			else wa = 1;
		}
		for (int j = 0; j < 2; j++){
			for (int z = 0; z < 2; z++){
				ans++;
				for (int o = 1; o < n; o++){
					if (x[o] == a[j]){
						if (x[o + 1] == 0 || x[o + 1] == b[z]){
							x[o + 1] = b[z];
						}
						else{
							ans--;
							break;
						}
					}
					else if (x[o] != a[j])
						continue;
				}
				memset(x,0,sizeof(x));
			}
		}
		if (wa == 1) cout << "0" << endl;
		else cout << ans % mod << endl;
		ans = 0;
	}
}
