#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int T = 1e5 + 1, F = -T, U = 0, N = 1e5 + 10;
int n, m, ans, f[N << 2]; bool p[N << 2];
inline ll read(){
ll x = 0, m = 1;
char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') m = -1;
ch = getchar();
}
while(isdigit(ch)){
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * m;
}
inline void write(ll x){
if(x < 0){
putchar('-');
write(-x);
return;
}
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
inline int find(int x, int sum = 0){
if(x == T || x == F) sum = x;
else if(p[n - x] || x == U) sum = U;
else if(p[x + n]) sum = T;
else if(x < 0){
if(x == -f[-x]) sum = x;
else p[x + n] = 1, sum = find(-f[-x]), p[x + n] = 0;
}else{
if(x == f[x]) sum = x;
else p[x + n] = 1, sum = f[x] = find(f[x]), p[x + n] = 0;
}
return sum;
}
inline void solve(){
n = read(), m = read(), ans = 0;
for(int i = 1; i <= n; ++ i) f[i] = i;
for(int i = 1; i <= m; ++ i){
int x; char ch; cin >> ch;
if(ch == 'T') f[read()] = T;
else if(ch == 'F') f[read()] = F;
else if(ch == 'U') f[read()] = U;
else if(ch == '+') f[read()] = f[read()];
else f[read()] = -f[read()];}
for(int i = 1; i <= n; ++ i) ans += (find(i) == U);
write(ans), putchar('\n');}
signed main(){
int c = read(), t = read();
while(t --) solve();
return 0;}