#include // iostream is too mainstream #include // bitch please #include #include #include #include #include #include #include #include #include #include #include #include #define dibs reserve #define OVER9000 1234567890 #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++) #define tisic 47 #define soclose 1e-8 #define chocolate win // so much chocolate #define patkan 9 #define ff first #define ss second #define abs(x) ((x < 0)?-(x):x) #define uint unsigned int #define dbl long double #define pi 3.14159265358979323846 using namespace std; // mylittledoge #ifdef DONLINE_JUDGE // palindromic tree is better than splay tree! #define lld I64d #endif int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int T; scanf(" %d",&T); for(int t =0; t < T; t++) { int N,M; scanf(" %d %d",&N,&M); int n =2*N+2; vector< vector > F(n,vector(n,0)); for(int i =0; i < N; i++) F[0][i+1] =F[N+i+1][n-1] =1; for(int i =0; i < M; i++) { int a,b; scanf(" %d %d",&a,&b); if(a != b) F[a][N+b] =1;} int f =0; while(true) { queue q; vector ako(n,-1); q.push(0); ako[0] =0; while(!q.empty()) { for(int i =0; i < n; i++) if(F[q.front()][i] > 0 && ako[i] == -1) { ako[i] =q.front(); q.push(i);} q.pop();} if(ako[n-1] == -1) break; f++; int akt =n-1; while(akt > 0) { F[ako[akt]][akt]--; F[akt][ako[akt]]++; akt =ako[akt];} } printf("%d\n",N-f);} return 0;} // look at my code // my code is amazing