#include <bits/stdc++.h>
using namespace std;
using lint         = long long;
constexpr lint inf = 1LL << 60;
constexpr lint mod = 1000000007;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    lint t;
    cin >> t;
    vector<lint> ret(t);
    for (int z = 0; z < t; ++z) {
        lint n;
        cin >> n;
        vector<lint> k(n), l(n), r(n);
        vector<array<lint, 4>> le, ri;
        for (int i = 0; i < n; ++i) {
            cin >> k[i] >> l[i] >> r[i];
            k[i]--;
            if (k[i] == n - 1)
                r[i] = l[i];
            if (l[i] > r[i]) {
                le.push_back({l[i] - r[i], -k[i], l[i], r[i]});
            } else {
                ri.push_back({r[i] - l[i], k[i], l[i], r[i]});
            }
        }
        sort(le.begin(), le.end(), greater<>());
        sort(ri.begin(), ri.end(), greater<>());
        lint nl        = le.size();
        set<lint> used = {-1, n};
        for (int i = 0; i < n; ++i) {
            used.insert(i);
        }
        lint res = 0;
        for (auto &l : le) {
            lint edge = min(nl - 1, -l[1]);
            lint idx  = *prev(used.upper_bound(edge));
            if (idx != -1) {
                res += l[2];
                used.erase(idx);
            } else {
                lint jdx = *prev(used.upper_bound(nl - 1));
                assert(jdx != -1);
                res += l[3];
                used.erase(jdx);
            }
        }
        for (auto &r : ri) {
            lint edge = max(nl, r[1]);
            lint idx  = *used.lower_bound(edge);
            if (idx != n) {
                res += r[3];
                used.erase(idx);
            } else {
                lint jdx = *used.lower_bound(nl);
                assert(jdx != n);
                res += r[2];
                used.erase(jdx);
            }
        }
        ret[z] = res;
    }

    for (auto &r : ret) {
        cout << r << "\n";
    }

    return 0;
}