#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(1e6)+7;
int n , q , res;
vector<int> g[maxN];
bool vis[maxN];
vector<ii> E;
struct DSU{
int fa[maxN] , cnt[maxN];
bool cycle;
void reset(){
cycle = 0;
for (int i = 1 ; i <= n ; i++) fa[i] = -1 , cnt[i] = 0;
}
int root(int x){
if (fa[x] < 0) return x; else return fa[x] = root(fa[x]);
}
void unite(int u , int v){
cnt[u]++;
cnt[v]++;
if (cnt[u] > 2) cycle = 1;
if (cnt[v] > 2) cycle = 1;
u = root(u);
v = root(v);
if (u == v){
cycle = 1;
return;
}
if (-fa[u] < -fa[v]) swap(u , v);
fa[u] += fa[v];
fa[v] = u;
}
int get(int x){
return -fa[root(x)];
}
} A , f[4];
vector<int> ver;
void solve(){
cin >> n >> q;
res = n;
bool check = 0;
A.reset();
for (int i = 0 ; i < 4 ; i++) f[i].reset();
int num_cycle = 0;
while (q--){
int u , v;
cin >> u;
if (u == -1){
cout << res << " ";
}
else{
cin >> v;
if (res == 0) continue;
if (check == 1){
res = 0;
for (int i = 0 ; i < sz(ver) ; i++){
if (u != ver[i] && v != ver[i]){
f[i].unite(u , v);
}
res += 1 - f[i].cycle;
}
}
else{
g[u].push_back(v);
g[v].push_back(u);
E.push_back({u , v});
if (sz(g[u]) < sz(g[v])) swap(u , v);
if (sz(g[u]) == 3){
res = 0;
ver = g[u]; ver.push_back(u);
for (int i = 0 ; i < sz(ver) ; i++){
for (auto it : E){
int u = it.fi;
int v = it.se;
if (u != ver[i] && v != ver[i]) f[i].unite(u , v);
}
res += 1 - f[i].cycle;
}
check = 1;
}
else{
if (A.root(u) == A.root(v)){
num_cycle++;
res = A.get(u);
}
A.unite(u , v);
if (num_cycle == 2) res = 0;
}
}
}
}
}
#define name "rings"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; cin >> t;
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBhbGwodikgdi5iZWdpbigpICwgdi5lbmQoKQojZGVmaW5lIHN6KHYpIGludCh2LnNpemUoKSkKI2RlZmluZSB1bnEodikgc29ydChhbGwodikpOyB2LnJlc2l6ZSh1bmlxdWUoYWxsKHYpKSAtIHYuYmVnaW4oKSk7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCAsIGludD4gaWk7CnR5cGVkZWYgcGFpcjxsb25nIGxvbmcgLCBpbnQ+IGxsaTsKCmNvbnN0IGludCBtYXhOID0gaW50KDFlNikrNzsKCmludCBuICwgcSAsIHJlczsKdmVjdG9yPGludD4gZ1ttYXhOXTsKYm9vbCB2aXNbbWF4Tl07CnZlY3RvcjxpaT4gRTsKCnN0cnVjdCBEU1V7CiAgICBpbnQgZmFbbWF4Tl0gLCBjbnRbbWF4Tl07CiAgICBib29sIGN5Y2xlOwoKICAgIHZvaWQgcmVzZXQoKXsKICAgICAgICBjeWNsZSA9IDA7CiAgICAgICAgZm9yIChpbnQgaSA9IDEgOyBpIDw9IG4gOyBpKyspIGZhW2ldID0gLTEgLCBjbnRbaV0gPSAwOwogICAgfQoKICAgIGludCByb290KGludCB4KXsKICAgICAgICBpZiAoZmFbeF0gPCAwKSByZXR1cm4geDsgZWxzZSByZXR1cm4gZmFbeF0gPSByb290KGZhW3hdKTsKICAgIH0KCiAgICB2b2lkIHVuaXRlKGludCB1ICwgaW50IHYpewogICAgICAgIGNudFt1XSsrOwogICAgICAgIGNudFt2XSsrOwogICAgICAgIGlmIChjbnRbdV0gPiAyKSBjeWNsZSA9IDE7CiAgICAgICAgaWYgKGNudFt2XSA+IDIpIGN5Y2xlID0gMTsKICAgICAgICB1ID0gcm9vdCh1KTsKICAgICAgICB2ID0gcm9vdCh2KTsKICAgICAgICBpZiAodSA9PSB2KXsKICAgICAgICAgICAgY3ljbGUgPSAxOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGlmICgtZmFbdV0gPCAtZmFbdl0pIHN3YXAodSAsIHYpOwogICAgICAgIGZhW3VdICs9IGZhW3ZdOwogICAgICAgIGZhW3ZdID0gdTsKICAgIH0KCiAgICBpbnQgZ2V0KGludCB4KXsKICAgICAgICByZXR1cm4gLWZhW3Jvb3QoeCldOwogICAgfQp9IEEgLCBmWzRdOwoKdmVjdG9yPGludD4gdmVyOwoKdm9pZCBzb2x2ZSgpewogICAgY2luID4+IG4gPj4gcTsKICAgIHJlcyA9IG47CiAgICBib29sIGNoZWNrID0gMDsKICAgIEEucmVzZXQoKTsKICAgIGZvciAoaW50IGkgPSAwIDsgaSA8IDQgOyBpKyspIGZbaV0ucmVzZXQoKTsKICAgIGludCBudW1fY3ljbGUgPSAwOwogICAgd2hpbGUgKHEtLSl7CiAgICAgICAgaW50IHUgLCB2OwogICAgICAgIGNpbiA+PiB1OwogICAgICAgIGlmICh1ID09IC0xKXsKICAgICAgICAgICAgY291dCA8PCByZXMgPDwgIiAiOwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBjaW4gPj4gdjsKICAgICAgICAgICAgaWYgKHJlcyA9PSAwKSBjb250aW51ZTsKICAgICAgICAgICAgaWYgKGNoZWNrID09IDEpewogICAgICAgICAgICAgICAgcmVzID0gMDsKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSAwIDsgaSA8IHN6KHZlcikgOyBpKyspewogICAgICAgICAgICAgICAgICAgIGlmICh1ICE9IHZlcltpXSAmJiB2ICE9IHZlcltpXSl7CiAgICAgICAgICAgICAgICAgICAgICAgIGZbaV0udW5pdGUodSAsIHYpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICByZXMgKz0gMSAtIGZbaV0uY3ljbGU7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgIGdbdV0ucHVzaF9iYWNrKHYpOwogICAgICAgICAgICAgICAgZ1t2XS5wdXNoX2JhY2sodSk7CiAgICAgICAgICAgICAgICBFLnB1c2hfYmFjayh7dSAsIHZ9KTsKICAgICAgICAgICAgICAgIGlmIChzeihnW3VdKSA8IHN6KGdbdl0pKSBzd2FwKHUgLCB2KTsKICAgICAgICAgICAgICAgIGlmIChzeihnW3VdKSA9PSAzKXsKICAgICAgICAgICAgICAgICAgICByZXMgPSAwOwogICAgICAgICAgICAgICAgICAgIHZlciA9IGdbdV07IHZlci5wdXNoX2JhY2sodSk7CiAgICAgICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDAgOyBpIDwgc3oodmVyKSA7IGkrKyl7CiAgICAgICAgICAgICAgICAgICAgICAgIGZvciAoYXV0byBpdCA6IEUpewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaW50IHUgPSBpdC5maTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGludCB2ID0gaXQuc2U7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAodSAhPSB2ZXJbaV0gJiYgdiAhPSB2ZXJbaV0pIGZbaV0udW5pdGUodSAsIHYpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIHJlcyArPSAxIC0gZltpXS5jeWNsZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgY2hlY2sgPSAxOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgICAgICBpZiAoQS5yb290KHUpID09IEEucm9vdCh2KSl7CiAgICAgICAgICAgICAgICAgICAgICAgIG51bV9jeWNsZSsrOwogICAgICAgICAgICAgICAgICAgICAgICByZXMgPSBBLmdldCh1KTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgQS51bml0ZSh1ICwgdik7CiAgICAgICAgICAgICAgICAgICAgaWYgKG51bV9jeWNsZSA9PSAyKSByZXMgPSAwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CgojZGVmaW5lIG5hbWUgInJpbmdzIgoKaW50IG1haW4oKXsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7IGNpbi50aWUoMCk7IGNvdXQudGllKDApOwogICAgaWYgKGZvcGVuKG5hbWUiLklOUCIgLCAiciIpKXsKICAgICAgICBmcmVvcGVuKG5hbWUiLklOUCIgLCAiciIgLCBzdGRpbik7CiAgICAgICAgZnJlb3BlbihuYW1lIi5PVVQiICwgInciICwgc3Rkb3V0KTsKICAgIH0KICAgIGludCB0ID0gMTsgY2luID4+IHQ7CiAgICBzb2x2ZSgpOwogICAgcmV0dXJuIDA7Cn0KCg==