description: Polycarp is very fond of playing the game Minesweeper. Recently he found a similar game and there are such rules.
  
  There are mines on the field, for each the coordinates of its location are known (x_i, y_i). Each mine has a lifetime in seconds, after which it will explode. After the explosion, the mine also detonates all mines vertically and horizontally at a distance of k (two perpendicular lines). As a result, we get an explosion on the field in the form of a "plus" symbol ('+'). Thus, one explosion can cause new explosions, and so on.
  
  Also, Polycarp can detonate anyone mine every second, starting from zero seconds. After that, a chain reaction of explosions also takes place. Mines explode instantly and also instantly detonate other mines according to the rules described above.
  
  Polycarp wants to set a new record and asks you to help him calculate in what minimum number of seconds all mines can be detonated.
  
  Input
  
  The first line of the input contains an integer t (1 ≤ t ≤ 10^4) — the number of test cases in the test.
  
  An empty line is written in front of each test suite.
  
  Next comes a line that contains integers n and k (1 ≤ n ≤ 2 ⋅ 10^5, 0 ≤ k ≤ 10^9) — the number of mines and the distance that hit by mines during the explosion, respectively.
  
  Then n lines follow, the i-th of which describes the x and y coordinates of the i-th mine and the time until its explosion (-10^9 ≤ x, y ≤ 10^9, 0 ≤ timer ≤ 10^9). It is guaranteed that all mines have different coordinates.
  
  It is guaranteed that the sum of the values n over all test cases in the test does not exceed 2 ⋅ 10^5.
  
  Output
  
  Print t lines, each of the lines must contain the answer to the corresponding set of input data — the minimum number of seconds it takes to explode all the mines.
  
  Example
  
  Input
  
  
  3
  
  5 0
  0 0 1
  0 1 4
  1 0 2
  1 1 3
  2 2 9
  
  5 2
  0 0 1
  0 1 4
  1 0 2
  1 1 3
  2 2 9
  
  6 1
  1 -1 3
  0 -1 9
  0 1 7
  -1 0 1
  -1 1 9
  -1 -1 7
  
  
  Output
  
  
  2
  1
  0
  
  Note
  
  <image> Picture from examples
  
  First example: 
  
    * 0 second: we explode a mine at the cell (2, 2), it does not detonate any other mine since k=0. 
    * 1 second: we explode the mine at the cell (0, 1), and the mine at the cell (0, 0) explodes itself. 
    * 2 second: we explode the mine at the cell (1, 1), and the mine at the cell (1, 0) explodes itself.
  
  
  
  Second example:
  
    * 0 second: we explode a mine at the cell (2, 2) we get: 
  
  <image>
  
    * 1 second: the mine at coordinate (0, 0) explodes and since k=2 the explosion detonates mines at the cells (0, 1) and (1, 0), and their explosions detonate the mine at the cell (1, 1) and there are no mines left on the field. 
solution: #include <bits/stdc++.h>
  using namespace std;
  int n, k, root[200005], d[200005];
  struct mine {
    long long x, y;
    int time, id;
    mine() { x = -1000000007, y = -1000000007, time = 0, id = 0; }
  } a[200005];
  long long dist(mine a, mine b) { return a.x - b.x + a.y - b.y; }
  int get_root(int u) { return (!root[u]) ? u : root[u] = get_root(root[u]); }
  void add(int u, int v) {
    u = get_root(u);
    v = get_root(v);
    if (u != v) {
      root[u] = v;
      d[v] = min(d[v], d[u]);
    }
  }
  void Solve() {
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
      root[i] = 0;
      cin >> a[i].x >> a[i].y >> a[i].time;
      d[i] = a[i].time;
      a[i].id = i;
    }
    sort(a + 1, a + n + 1, [](mine a, mine b) {
      if (a.x == b.x) return a.y < b.y;
      return a.x < b.x;
    });
    for (int i = 1; i <= n; ++i) {
      if (a[i].x == a[i - 1].x) {
        if (dist(a[i], a[i - 1]) <= k) {
          add(a[i].id, a[i - 1].id);
        }
      }
    }
    sort(a + 1, a + n + 1, [](mine a, mine b) {
      if (a.y == b.y) return a.x < b.x;
      return a.y < b.y;
    });
    for (int i = 1; i <= n; ++i) {
      if (a[i].y == a[i - 1].y) {
        if (dist(a[i], a[i - 1]) <= k) {
          add(a[i].id, a[i - 1].id);
        }
      }
    }
    sort(a + 1, a + n + 1, [](mine a, mine b) { return a.time < b.time; });
    vector<int> v;
    for (int i = 1; i <= n; ++i) {
      v.push_back(get_root(i));
    }
    sort(v.begin(), v.end());
    v.resize(unique(v.begin(), v.end()) - v.begin());
    for (auto &x : v) {
      x = d[x];
    }
    sort(v.begin(), v.end());
    int run = 0;
    for (int i = 0; i < v.size(); ++i) {
      while (run < v.size() && v[run] <= i) ++run;
      if (run + i + 1 >= v.size()) {
        cout << i << '\n';
        return;
      }
    }
  }
  int main() {
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    if (fopen(""
              ".inp",
              "r")) {
      freopen(
          ""
          ".inp",
          "r", stdin);
      freopen(
          ""
          ".out",
          "w", stdout);
    }
    int t;
    cin >> t;
    while (t--) {
      Solve();
    }
    cerr << "Time collapse : " << fixed << setprecision(3)
         << 1.000 * clock() / CLOCKS_PER_SEC;
    return 0;
  }
  