#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int>> vvi;
bool v[10005];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t, n, m, a, b;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    vvi g(n, vector<int>(0));
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      a--; b--;
      g[a].push_back(b);
      g[b].push_back(a);
    }
    memset(v, 0, sizeof (v));
    queue<int> q;
    int count = 0;
    for (int i = 0; i < n; i++) {
      q.push(i);
      while (!q.empty()) {
        int t = q.front();
        q.pop();
        if (v[t]) {
          continue;
        }
        v[t] = true;
        count++;
        for (auto e : g[t]) {
          if (!v[e]) {
            q.push(e);
            break;
          }
        }
      }

    }
    cout << count - 1 << '\n';
  }
  return 0;
}