cloud_9 @ 2018-11-28 20:43:24
#include<bits/stdc++.h>
using namespace std;
const int mxn=2e6+5;
int n,m,s,flag,cnt,col,ins[mxn],dfn[mxn],low[mxn],bl[mxn];
vector<int >g[mxn];stack<int >st;
string s1,s2;
inline int read()
{
char c=getchar();int x=0,f=1;
while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
return x*f;
}
void tj(int u)
{
dfn[u]=low[u]=++s,st.push(u),ins[u]=1;
for(int i=0;i<g[u].size();++i){
int v=g[u][i];
if(!dfn[v]) tj(v),low[u]=min(low[u],low[v]);
else if(ins[v]) low[u]=min(dfn[v],low[u]);
}
if(dfn[u]==low[u]){
++cnt;
do{
bl[u]=cnt;
u=st.top();
st.pop();
ins[u]=0;
} while(low[u]!=dfn[u]);
}
}
int main()
{
int T;cin>>T;
int x,y,xi,yi;
while(T--){
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(bl,0,sizeof(bl));memset(ins,0,sizeof(bl));
//memset(bl,0,sizeof(bl));
col=cnt=s=0;
// n=read(),m=read();
cin>>n>>m;
for(int i=1;i<=m;++i){
// x=read(),xi=read(),y=read(),yi=read();
cin>>s1>>s2;
x=s1[1]-'0',xi=(s1[0]=='m' ? 1 : 0);y=s2[1]-'0';yi=(s2[0]=='m'?1:0);
// cout<<s1[0];
g[x+n*(xi^1)].push_back(y+yi*n);
g[y+n*(yi^1)].push_back(x+xi*n);
}
flag=0;
for(int i=1;i<=2*n;++i) if(!dfn[i]) tj(i);
while(!st.empty()) st.pop();
for(int i=1;i<=n*2;++i) g[i].clear();
for(int i=1;i<=n;++i)
if(bl[i]==bl[i+n]) {
printf("BAD\n");
flag=1;
break;
}
if(flag==0)
printf("GOOD\n");
/// for(int i=1;i<=n*2;++i)
// printf("%d ",bl[i]);
// }
}
return 0;
}