#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int T, N;
  for (cin >> T; T--;) {
    cin >> N;
    vector<int> X(N), Y(N);
    for (int i = 0; i < N; i++) cin >> X[i] >> Y[i];

    vector<int> xs = X, ys = Y;
    sort(xs.begin(), xs.end());
    sort(ys.begin(), ys.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());
    if (xs.size() == 1 || ys.size() == 1) { cout << 1 << endl; continue; }

    vector<vector<int>> xe(xs.size()), ye(ys.size());
    for (int i = 0; i < N; i++) {
      int x = lower_bound(xs.begin(), xs.end(), X[i]) - xs.begin();
      int y = lower_bound(ys.begin(), ys.end(), Y[i]) - ys.begin();
      xe[x].push_back(y);
      ye[y].push_back(x);
    }

    vector<int> xmat(xs.size(), -1), ymat(ys.size(), -1);
    for (;;) {
      vector<int> q, nexty, yu(ys.size());
      vector<vector<int>> yp(ys.size());
      for (int x = 0; x < xs.size(); x++) if (xmat[x] == -1) q.push_back(x);
      while (!q.empty() && nexty.empty()) {
        vector<int> q2;
        for (auto x : q) for (auto y : xe[x]) {
          yp[y].push_back(x);
          if (yu[y]) continue;
          yu[y] = true;
          if (ymat[y] == -1) nexty.push_back(y); else q2.push_back(ymat[y]);
        }
        q.swap(q2);
      }
      if (nexty.empty()) break;
      yu = vector<int>(ys.size());
      function<bool(int)> doit = [&](int y) {
        if (yu[y] == true) return false;
        yu[y] = true;
        for (auto x : yp[y]) {
          if (xmat[x] == -1 || doit(xmat[x])) { xmat[x] = y; ymat[y] = x; return true; }
        }
        return false;
      };
      for (auto y : nexty) doit(y);
    }

    int rx = 0, ry = 0, ret = 0;
    vector<bool> xu(xs.size()), yu(ys.size());
    function<void(int, vector<bool>&, const vector<int>&, const vector<vector<int>>&)> rec =
        [&](int i, vector<bool>& u, const vector<int>& mat, const vector<vector<int>>& e) {
      if (u[i]) return;
      u[i] = true;
      ret++;
      for (auto j : e[mat[i]]) rec(j, u, mat, e);
    };
    for (int x = 0; x < xs.size(); x++) if (xmat[x] == -1) { ry = 1; for (auto y : xe[x]) rec(y, yu, ymat, xe); }
    for (int y = 0; y < ys.size(); y++) if (ymat[y] == -1) { rx = 1; for (auto x : ye[y]) rec(x, xu, xmat, ye); }

    for (int x = 0; x < xs.size(); x++) if (xmat[x] != -1 && !xu[x] && !yu[xmat[x]]) {
      if (rx == 0 && ry == 0) {
        int oret = ret;
        vector<bool> oxu = xu, oyu = yu;
        rec(x, xu, xmat, ye);
        int ret1 = ret;
        ret = oret; xu = oxu;
        rec(xmat[x], yu, ymat, xe);
        if (ret1 < ret) {
          ret = oret; yu = oyu;
          rx = 1;
          rec(x, xu, xmat, ye);
        } else {
          ry = 1;
        }
      } else if (rx == 0) {
        rx = 1;
        rec(x, xu, xmat, ye);
      } else {
        ry = 1;
        rec(xmat[x], yu, ymat, xe);
      }
    }

    cout << ret + (!rx || !ry) << endl;
  }
}
