#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <set>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <map>
using namespace std;
/*long long int fact(long long int a)
{
if(a==0||a==1)
return 1;
else
{
long long int ans=1;
for(long long int i=a;i>=1;i--)
{
ans=(ans%1000000007)*(i%1000000007);
ans=(ans%1000000007);
}
return ans;
}
}
int cmpfunc (const void * a, const void * b)
{
if( *(long long int*)a - *(long long int*)b < 0 )
return -1;
if( *(long long int*)a - *(long long int*)b > 0 )
return 1;
if( *(long long int*)a - *(long long int*)b == 0 )
return 0;
}*/
int a[100000];
int main()
{
long long int t;
cin>>t;
while(t--)
{
getchar();
long long int n,k;
cin>>n>>k;
for(long long int i=1;i<=n;i++)
a[i]=0;
for(long long int i=0;i<k;i++)
{
long long int weight;
cin>>weight;
long int left[10000],right[10000];
for(long long int j=0;j<weight;j++)
cin>>left[j];
for(long long int j=0;j<weight;j++)
cin>>right[j];
char comp;
cin>>comp;
if(comp=='=')
{
for(long long int k=0;k<weight;k++)
{
a[left[k]]=1;
a[right[k]]=1;
}
}
//else if ()
}
long long int ans=0,count=0;
for(long long int i=1;i<=n;i++)
{
if(a[i]==0)
{
count++;
ans=i;
}
}
if(count==1)
cout<<ans<<endl;
else
cout<<0<<endl
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPiAgICAgCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8c3RyaW5nLmg+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjc3RyaW5nPgojaW5jbHVkZSA8Y3R5cGUuaD4KI2luY2x1ZGUgPG1hcD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAogCi8qbG9uZyBsb25nIGludCBmYWN0KGxvbmcgbG9uZyBpbnQgYSkKewoJaWYoYT09MHx8YT09MS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