#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int M = 1e9 + 7;
int t,c,n,k,x,y,q[1000001];
ll ans;
int main()
{
	freopen("traverse.in","r",stdin);
	freopen("traverse.out","w",stdout);
	scanf("%d%d",&c,&t);
	while(t--)
	{
		scanf("%d%d",&n,&k);
		for(int i = 1;i <= n;i++)
		{
			scanf("%d%d",&x,&y);
			ans ^= x;
			ans ^= (~y);
		}
		for(int i = 1;i <= k;i++)
		{
			scanf("%d",&q[i]);
			ans ^= q[i];
		}
		cout  << (ans + M) % M << "\n";
	}
	return 0;
}
