#include <iostream>
using namespace std;
#include <unordered_set>
const int N = 1010;
int t;
int n,m;
int p[N];
int a,b;
// 并查集
int find(int x)
{
if(p[x]!=x)p[x] = find(p[x]);
return p[x];
}
int main()
{
cin >> t;
while(t--)
{
cin >> n >> m;
unordered_set<int>st;
for(int i = 1; i <= n;i++)p[i] = i;
int t = n;
while(m--)
{
cin >> a >> b;
a = find(a);
b = find(b);
if(a!=b)
{
p[b] = a;
t--;
}
}
// 统计祖先的个,这个做法不对,不知道为啥~~希望有大佬帮我解答一下
for(int i = 1;i<=n;i++)
{
st.insert(p[i]);
}
// 这个做法是对的:有合并就让总桌子数-1
cout << t << endl;
}
return 0;
}