#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>
#include <random>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
clock_t __my_start_clock = clock();
#else
#define eprintf(...)
#endif

#define TIMESTAMP(x) eprintf("["#x"] Time : %.3lf s.\n", (clock() - __my_start_clock)*1.0/CLOCKS_PER_SEC)
#define TIMESTAMPf(x,...) eprintf("[" x "] Time : %.3lf s.\n", __VA_ARGS__, (clock() - __my_start_clock)*1.0/CLOCKS_PER_SEC)

#define db(x) cerr << #x << " = " << x << endl
#define db2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define db3(x, y, z) cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", " << z << ")\n"
#define dbv(a) cerr << #a << " = "; for (auto xxxx: a) cerr << xxxx << " "; cerr << endl


using namespace std;

#ifdef LOCAL
static struct __timestamper {
    string what;
    __timestamper(const char* what) : what(what){};
    __timestamper(const string& what) : what(what){};
    ~__timestamper(){
        TIMESTAMPf("%s", what.data());
    }
} __TIMESTAMPER("end");
#else
struct __timestamper {};
#endif

typedef long long ll;
typedef long double dbl;

const int MOD = 998244353;

void add(int &x, int y) {
    x += y;
    if (x >= MOD) x -= MOD;
}

int mul(int x, int y) {
    return 1LL * x * y % MOD;
}

struct circle {
    int x, y, r;
    int id;

    circle operator- (const circle &c) const {
        circle res;
        res.x = x - c.x;
        res.y = y - c.y;
        return res;
    }

    ll d2() {
        return 1LL * x * x + 1LL * y * y;
    }

    double get_y(int tx) const {
        assert(abs(x - tx) <= r);
        if (abs(x - tx) == r) return y;

        // (x - tx)^2 + (y - ty)^2 = r^2
        // (y - ty)^2 = r^2 - (x - tx)^2
        // |y - ty| = sqrt(r^2 - (x - tx)^2)
        double dy2 = 1.0 * r * r - 1.0 * (x - tx) * (x - tx);
        assert(dy2 >= -1e-9);
        double dy = sqrt(max(dy2, 0.0));
        return y - dy;
    }
};

bool intersect(circle c1, circle c2) {
    if (c1.r < c2.r) swap(c1, c2);
    if ((c1 - c2).d2() <= 1LL * c1.r * c1.r) {
        return 1LL * (c1.x - c2.x) * (c1.x - c2.x) + 1LL * (c1.y - c2.y) * (c1.y - c2.y) == 1LL * (c1.r - c2.r) * (c1.r - c2.r);
    } else {
        return 1LL * (c1.x - c2.x) * (c1.x - c2.x) + 1LL * (c1.y - c2.y) * (c1.y - c2.y) == 1LL * (c1.r + c2.r) * (c1.r + c2.r);
    }
}

ll solve(int n, vector<circle> a) {
    int cur_x;
    auto cmp = [&](const circle &c1, const circle &c2) {
        double y1 = c1.get_y(cur_x);
        double y2 = c2.get_y(cur_x);
        if (y1 != y2) return y1 < y2;
        return c1.id < c2.id;
    };
    set<circle, decltype(cmp)> st(cmp);
    map<int, vector<int>> mp;
    for (int i = 0; i < n; i++) {
        mp[a[i].x - a[i].r].push_back(i);
        mp[a[i].x + a[i].r].push_back(~i);
    }

    vector<pair<int, int>> ans;
    auto push = [&](pair<int, int> o) {
        if (o.first > o.second) swap(o.first, o.second);
        ans.push_back(o);
    };

    for (auto it : mp) {
        auto vct = it.second;
        sort(vct.begin(), vct.end());
        cur_x = it.first;

        for (int id : vct) {
//            cout << cur_x << " " << (id >= 0 ? "+" : "-") << (id >= 0 ? id : ~id) << endl;

            if (id >= 0) {
                auto it = st.lower_bound(a[id]);
                if (it != st.end()) {
                    if (intersect(a[id], *it)) ans.push_back({a[id].id, it->id});
                }
                if (it != st.begin()) {
                    it--;
                    if (intersect(a[id], *it)) ans.push_back({a[id].id, it->id});
                }
            }
        }
        for (auto id : vct) {
            if (id < 0) {
                auto it = st.lower_bound(a[~id]);
//                if (it->id != ~id) {
//                    cout << a[~id].x << " " << a[~id].y << " " << a[~id].r << endl;
//                    cout << it->x << " " << it->y << " " << it->r << endl;
//                }
                assert(it->id == ~id);
                it = st.erase(it);
                if (it != st.end() && it != st.begin()) {
                    auto it2 = it;
                    it2--;
                    if (intersect(*it, *it2)) {
                        ans.push_back({it->id, it2->id});
                    }
                }
            }
        }
        for (auto id : vct) {
            if (id >= 0) {
                auto it = st.insert(a[id]).first;
                if (it != st.begin()) {
                    auto it2 = it;
                    it2--;
                    if (intersect(*it, *it2)) {
                        ans.push_back({it->id, it2->id});
                    }
                }
                {
                    auto it2 = it;
                    it++;
                    if (it != st.end() && intersect(*it, *it2)) {
                        ans.push_back({it->id, it2->id});
                    }
                }
            }
        }
    }

    sort(ans.begin(), ans.end());
    ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
    return ans.size();
}

int main() {
#ifdef LOCAL
    freopen("in", "r", stdin);
#endif

    int T;
    scanf("%d", &T);
    while (T--) {
        int n;
        scanf("%d", &n);
        vector<circle> a(n);
        for (int i = 0; i < n; i++) {
            scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].r);
            a[i].id = i;
        }

        printf("%lld\n", solve(n, a));
    }

    return 0;
}