#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
vvii adj; vi col;
// 0 human, 1 parasite
int dfs1(int u, int c){
col[u] = c; int ans = c;
for(auto pp : adj[u]){
int v = pp.first, w = pp.second;
if(col[v] == -1){
int jp = dfs1(v, c^w);
if(jp == -1) return -1;
ans += jp;
}else if(col[v] != (c^w)) return -1;
}
return ans;
}
void clear_dfs(int u){
col[u] = -1;
for(auto pp : adj[u]){
int v = pp.first;
if(col[v] != -1) clear_dfs(v);
}
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int T; cin >> T;
while(T--){
int N, Q; cin >> N >> Q;
adj.resize(N); col.resize(N, -1);
vi cnt(N);
while(Q--){
int k, a, b; cin >> k >> a >> b;
if(k == 2) k = 0;
adj[a-1].push_back({b-1, k});
adj[b-1].push_back({a-1, k});
}
int ans = 0;
for(int x = 0; x < N; x++){
if(col[x] != -1) continue;
int tmp = -1;
// set as 0
tmp = max(tmp, dfs1(x, 0));
clear_dfs(x);
// set as 1
tmp = max(tmp, dfs1(x, 1));
if(tmp == -1) {ans = -1; break;}
ans += tmp;
}
cout << ans << "\n";
adj.clear(); col.clear();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIGxvbmcgZG91YmxlIGxkOwoKdHlwZWRlZiBwYWlyPGludCwgaW50PiBpaTsKdHlwZWRlZiB2ZWN0b3I8aW50PiB2aTsKdHlwZWRlZiB2ZWN0b3I8aWk+IHZpaTsKdHlwZWRlZiB2ZWN0b3I8dmk+IHZ2aTsKdHlwZWRlZiB2ZWN0b3I8dmlpPiB2dmlpOwoKdnZpaSBhZGo7IHZpIGNvbDsKLy8gMCBodW1hbiwgMSBwYXJhc2l0ZQoKaW50IGRmczEoaW50IHUsIGludCBjKXsKICAgIGNvbFt1XSA9IGM7IGludCBhbnMgPSBjOwogICAgZm9yKGF1dG8gcHAgOiBhZGpbdV0pewogICAgICAgIGludCB2ID0gcHAuZmlyc3QsIHcgPSBwcC5zZWNvbmQ7CiAgICAgICAgCiAgICAgICAgaWYoY29sW3ZdID09IC0xKXsKICAgICAgICAgICAgaW50IGpwID0gZGZzMSh2LCBjXncpOwogICAgICAgICAgICBpZihqcCA9PSAtMSkgcmV0dXJuIC0xOwogICAgICAgICAgICBhbnMgKz0ganA7CiAgICAgICAgfWVsc2UgaWYoY29sW3ZdICE9IChjXncpKSByZXR1cm4gLTE7CiAgICB9CiAgICByZXR1cm4gYW5zOwp9Cgp2b2lkIGNsZWFyX2RmcyhpbnQgdSl7CiAgICBjb2xbdV0gPSAtMTsKICAgIGZvcihhdXRvIHBwIDogYWRqW3VdKXsKICAgICAgICBpbnQgdiA9IHBwLmZpcnN0OwoKICAgICAgICBpZihjb2xbdl0gIT0gLTEpIGNsZWFyX2Rmcyh2KTsKICAgIH0KICAgIHJldHVybjsKfQoKaW50IG1haW4oKXsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoTlVMTCk7CgogICAgaW50IFQ7IGNpbiA+PiBUOwogICAgd2hpbGUoVC0tKXsKICAgICAgICBpbnQgTiwgUTsgY2luID4+IE4gPj4gUTsKICAgICAgICBhZGoucmVzaXplKE4pOyBjb2wucmVzaXplKE4sIC0xKTsKCiAgICAgICAgdmkgY250KE4pOwogICAgICAgIHdoaWxlKFEtLSl7CiAgICAgICAgICAgIGludCBrLCBhLCBiOyBjaW4gPj4gayA+PiBhID4+IGI7CiAgICAgICAgICAgIGlmKGsgPT0gMikgayA9IDA7CiAgICAgICAgICAgIGFkalthLTFdLnB1c2hfYmFjayh7Yi0xLCBrfSk7CiAgICAgICAgICAgIGFkaltiLTFdLnB1c2hfYmFjayh7YS0xLCBrfSk7CiAgICAgICAgfQoKICAgICAgICBpbnQgYW5zID0gMDsKICAgICAgICBmb3IoaW50IHggPSAwOyB4IDwgTjsgeCsrKXsKICAgICAgICAgICAgaWYoY29sW3hdICE9IC0xKSBjb250aW51ZTsKICAgICAgICAgICAgaW50IHRtcCA9IC0xOwogICAgICAgICAgICAvLyBzZXQgYXMgMAogICAgICAgICAgICB0bXAgPSBtYXgodG1wLCBkZnMxKHgsIDApKTsKICAgICAgICAgICAgY2xlYXJfZGZzKHgpOwoKICAgICAgICAgICAgLy8gc2V0IGFzIDEKICAgICAgICAgICAgdG1wID0gbWF4KHRtcCwgZGZzMSh4LCAxKSk7CgogICAgICAgICAgICBpZih0bXAgPT0gLTEpIHthbnMgPSAtMTsgYnJlYWs7fQogICAgICAgICAgICBhbnMgKz0gdG1wOwogICAgICAgIH0KCiAgICAgICAgY291dCA8PCBhbnMgPDwgIlxuIjsKCiAgICAgICAgYWRqLmNsZWFyKCk7IGNvbC5jbGVhcigpOwoKICAgIH0KCgogICAgcmV0dXJuIDA7Cn0=