1 /*
2 ID: asif
3 LANG: C++
4 TASK: test
5 */
6 //# pragma comment(linker, "/STACK:102400000,102400000")
7 # include<iostream>
8 # include<cstdio>
9 # include<cstdlib>
10 # include<cstring>
11 # include<algorithm>
12 # include<cctype>
13 # include<cmath>
14 # include<string>
15 # include<set>
16 # include<map>
17 # include<stack>
18 # include<queue>
19 # include<vector>
20 # include<numeric>
21 using namespace std;
22 const int maxn=100010;
23 const double inf=0.000001;
24 const int INF=~0U>>1;
25 const int mod=1000000007;
26 # define PI (acos(0)*2.0)
27 # define lson l,m,rt<<1
28 # define rson m+1,r,rt<<1 | 1
29 # define PS printf("\n")
30 # define S(n) scanf("%d",&n)
31 # define P(n) printf("%d\n",n)
32 # define Ps(n) printf(" %d",(n))
33 # define SB(n) scanf("%lld",&n)
34 # define PB(n) printf("%lld\n",n)
35 # define PBs(n) printf(" %lld",n)
36 # define SD(n) scanf("%lf",&n)
37 # define PD(n) printf("%.3lf\n",n)
38 # define Sstr(s) scanf("%s",s)
39 # define Pstr(s) printf("%s\n",s)
40 # define S0(a) memset(a,0,sizeof(a))
41 # define S1(a) memset(a,-1,sizeof(a))
42 typedef long long ll;
43 int n,f[maxn],p[maxn],m;
44 int equal(double x,double y)
45 {
46 if(x-y>=-inf&&x-y<=inf)
47 return 0;
48 else if(x-y>inf)
49 return 1;
50 return -1;
51 }
52 void init()
53 {
54 for(int i=0;i<=n;i++)
55 f[i]=i,p[i]=0;
56 }
57 int find(int x)
58 {
59 if(x==f[x])
60 return x;
61 int fx=find(f[x]);
62 p[x]=(p[x]+p[f[x]])%3;
63 f[x]=fx;
64 return f[x];
65 }
66 bool unin(int a,int x,int y)
67 {
68 a--;
69 int fx=find(x);
70 int fy=find(y);
71 if(fx==fy)
72 {
73 if((p[y]-p[x]+3)%3!=a)
74 return true;
75 return false;
76 }
77 f[fy]=fx;
78 p[fy]=(p[x]-p[y]+3+a)%3;
79 return false;
80 }
81 int main()
82 {
83 //freopen("input.in", "r", stdin);
84 //freopen("output.out", "w", stdout);
85 int T;
86 S(T);
87 while(T--)
88 {
89 S(n),S(m);
90 init();
91 int ans=0;
92 while(m--)
93 {
94 int a,u,v;
95 S(a),S(u),S(v);
96 if(u>n||v>n||(a==2&&u==v))
97 ans++;
98 else
99 {
100 if(unin(a,u,v))
101 ans++;
102 }
103 }
104 P(ans);
105 }
106 return 0;
107 }