#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAXN 1000+5
#define DEBUG 1
int PRE[MAXN] = {0};
int T,N,M;
void init(){
for(int i=0;i<MAXN;i++)
PRE[i] = i;
N=M =0;
}
int find_x(int x){
if(PRE[x] == x)
return x;
else{
return PRE[x] = find_x(PRE[x]);
}
}
void union_set(int x,int y){
int t1 = find_x(x);
int t2 = find_x(y);
if(t1 != t2){
PRE[t2] = t1;
}
}
int main()
{
if(DEBUG)
freopen("data.txt","r",stdin);
cin>>T;
while(T--){
init();
cin>>N>>M;
int a,b;
for(int i=0;i<M;i++){
cin>>a>>b;
union_set(a,b);
}
int cnt = 0;
for(int i=1;i<=N;i++){
if(PRE[i] == i)
cnt++;
}
cout<<cnt<<endl;
}
//cout << "Hello world!" << endl;
return 0;
}