#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define PII pair<int,int>
#define fir first
#define sec second
using namespace std;
const int N=1e5+7;
int n,m;
int cc,tt;
PII f[N];
PII g[N];
char s[N];
vector<PII> q[N];
inline int find(int x){
if(f[x].fir==x){
return x;
}
return f[x].fir=find(f[x].fir);
}
inline char calc(int i){
if(s[i]=='T'){
return 'F';
}
else if(s[i]=='F'){
return 'T';
}
return s[i];
}
inline void init(){
memset(s,'\0',sizeof s);
for(int i=0;i<=n;i++){
q[i].clear();
}
for(int i=1;i<=n;i++){
f[i]={i,0};
}
return ;
}
inline PII total(int h,int e){
if(f[h].fir==h){
return {h,e};
}
int r=find(h);
if(e==-2){
for(auto i:q[h]){
if(i.fir==r){
e=i.sec;
break;
}
}
}
else{
for(auto i:q[h]){
if(i.fir==r){
int v=i.sec;
if(f[r].fir==r){
return {r,e};
}
else{
if(v==1&&e==1){
e=1;
}
else if(v==2&&e==1){
e=2;
}
else if(v==1&&e==2){
e=2;
}
else if(v==2&&e==2){
e=1;
}
}
return total(v,e);
}
}
}
return {0,0};
}
inline void print(){
for(int i=1;i<=n;i++){
printf("i=%d,fa=%d,relation=%d\n",i,f[i].fir,g[i].sec);
}
return ;
}
signed main(){
freopen("tribool.in","r",stdin);
freopen("tribool.out","w",stdout);
cin.tie(NULL);
scanf("%d%d",&cc,&tt);
while(tt--){
scanf("%d%d",&n,&m);
init();
char op;
int x,y;
for(int i=0;i<m;i++){
cin>>op;
if(op=='T'||op=='F'||op=='U'){
scanf("%d",&x);
s[x]=op;
}
else if(op=='+'){
scanf("%d%d",&x,&y);
q[x].push_back({y,1});
f[x]={find(y),1};
}
else if(op=='-'){
scanf("%d%d",&x,&y);
q[x].push_back({y,2});
f[x]={find(y),2};
}
}
for(int i=1;i<=n;i++){
if(f[i].fir!=i){
g[i]=total(i,-2);
}
}
printf("%d\n",f[1].fir);
}
fclose(stdin);
fclose(stdout);
return 0;
}