/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>
typedef long long LL;
using namespace std;
int n,m;
int f[1005];
int Find(int x){
return x == f[x] ? x : Find(f[x]);
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int t;
scanf("%d", &t);
while(t--){
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) f[i] = i;
int x, y;
for (int i = 1; i <= m; i++){
scanf("%d %d", &x, &y);
int xx = Find(x), yy = Find(y);
if(xx != yy){
f[xx] = yy;
}
}
set<int>st;
for (int i = 1; i <= n; i++){
f[i] = Find(i);
st.insert(f[i]);
}
printf("%d\n", st.size());
}
return 0;
}
/**/