#include <iostream>
using namespace std;
const int N=1001;
int bleg[N];
int find (int x){
int y=x;
while (y!=bleg[y])
y=bleg[y];
while (x!=bleg[x]){
int px=bleg[x];
bleg[x]=y;
x=px;
}
return y;
}
void Union (int a,int b){
int pa=find(a),pb=find(b);
if (pa!=pb) bleg[pa]=pb;
}
void init (int n){
for (int i=1;i<=n;i++) bleg[i]=i;
}
int main (){
int t;
cin>>t;
while (t--){
int n,m,a,b;
cin>>n>>m;
init (n);
for (int i=1;i<=m;i++){
cin>>a>>b;
Union (a,b);
}
int cnt=0;
for (int i=1;i<=n;i++)
if (find(i)==i)
cnt++;
cout<<cnt<<endl;
// if (t)
// cout<<endl;
}
return 0;
}
#include using namespace std;const int N=1001;int bleg[N];int find (int x){ int y=x; while (y!=bleg[y]) y=bleg[y]; while (x!=bleg[x]){ int px=bleg[x]; bleg[x]=y; x=px; } return y;}v