/**
 *    author:  tourist
 *    created: 08.11.2024 23:02:23
**/
#undef _GLIBCXX_DEBUG

#pragma GCC optimize("O3,unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<int64_t> x(n), y(n), r(n);
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i] >> r[i];
    }
    int64_t ans = 0;
    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 1; j < n; j++) {
        auto dist = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
        if (dist == (r[i] + r[j]) * (r[i] + r[j])) {
          ans += 1;
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
