#include <bits/stdc++.h>
using namespace std;
int fa[1000000];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int main() {
  int n, m, a, b, i, t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (i = 1; i <= n; i++) fa[i] = i;
    for (i = 0; i < m; i++) {
      cin >> a >> b;
      a = find(a);
      b = find(b);
      if (a != b) fa[a] = b;
    }
    int s = 0;
    for (i = 1; i <= n; i++)
      if (fa[i] == i) s++;
    printf("%d\n", s);
  }
}
