#include <bits/stdc++.h>
#define FASTIO
using namespace std;

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

template <class T, class Op>
class SegmentTree {
private:
    const Op op;
    const T id;
    std::vector<T> vec;
    int sz;

public:
    explicit SegmentTree(int n, Op op, T id) noexcept
        : op(op), id(id) {
        sz = 1;
        while (sz < n) sz <<= 1;
        vec.assign(sz * 2, id);
    }
    explicit SegmentTree(const std::vector<T>& vec, Op op, T id) noexcept
        : op(op), id(id) {
        int n = vec.size();
        sz = 1;
        while (sz < n) sz <<= 1;
        this->vec.assign(sz * 2, id);
        set_array(vec);
    }
    void set_value(int idx, T val) noexcept {
        vec[idx + sz] = val;
    }
    template <class RandomIt>
    void set_array(RandomIt _begin, RandomIt _end) noexcept {
        std::copy(_begin, _end, vec.begin() + sz);
    }
    void set_array(const std::vector<T>& vec) noexcept {
        set_array(vec.begin(), vec.end());
    }
    void build() noexcept {
        for (int i = sz - 1; i > 0; i--) {
            vec[i] = op(vec[i * 2], vec[i * 2 + 1]);
        }
    }
    void update(int idx, T val) noexcept {
        idx += sz;
        vec[idx] = val;
        for (idx >>= 1; idx > 0; idx >>= 1) {
            vec[idx] = op(vec[idx * 2], vec[idx * 2 + 1]);
        }
    }
    // Query [l, r)
    T query(int l, int r) const noexcept {
        T l_val = id, r_val = id;
        l += sz, r += sz - 1;
        for (; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) l_val = op(l_val, vec[l++]);
            if (!(r & 1)) r_val = op(r_val, vec[r--]);
        }
        return op(l_val, r_val);
    }
    const T& operator[](int idx) const noexcept {
        return vec[idx + sz];
    }
    void reset() noexcept {
        std::fill(vec.begin(), vec.end(), id);
    }
};

void solve() {
    int Q;
    cin >> Q;
    using Tup = tuple<ll, ll, ll>;
    vector<Tup> va, vb, vc;
    while (Q--) {
        ll N;
        cin >> N;
        va.clear(), vb.clear(), vc.clear();
        for (ll i = 0; i < N; i++) {
            ll k, l, r;
            cin >> k >> l >> r;
            --k;
            if (k == N - 1 || l == r) {
                vc.emplace_back(k, l, r);
            }
            else if (l > r) {
                va.emplace_back(k, l, r);
            }
            else {
                vb.emplace_back(k, l, r);
            }
        }
        sort(va.begin(), va.end(), [](Tup& l, Tup& r) -> bool {
            auto& [kl, ll, rl] = l;
            auto& [kr, lr, rr] = r;
            if (ll - rl < lr - rr) {
                return true;
            }
            else {
                return false;
            }
        });
        sort(vb.begin(), vb.end(), [](Tup& l, Tup& r) -> bool {
            auto& [kl, ll, rl] = l;
            auto& [kr, lr, rr] = r;
            if (ll - rl > lr - rr) {
                return true;
            }
            else {
                return false;
            }
        });

        ll na = (ll)va.size();
        ll nb = (ll)vb.size();
        ll nc = (ll)vc.size();

        auto op1 = [](ll l, ll r) { return max(l, r); };
        SegmentTree seg1(N, op1, -1);

        auto op2 = [](ll l, ll r) { return min(l, r); };
        SegmentTree seg2(N, op2, N);

        for (ll i = 0; i < N; i++) {
            seg1.set_value(i, i);
            seg2.set_value(i, i);
        }
        seg1.build();
        seg2.build();

        ll tail1 = na - 1;
        ll head2 = na + nc - 1;

        ll ans = 0;

        for (ll i = na - 1; i >= 0; i--) {
            auto [k, l, r] = va.back();
            va.pop_back();
            ll d = seg1.query(0, min(k + 1, tail1) + 1);
            if (d >= 0) {
                ans += l;
                seg1.update(d, -1);
            }
            else {
                ans += r;
                --tail1;
            }
        }

        for (ll i = N - nb; i < N; i++) {
            auto [k, l, r] = vb.back();
            vb.pop_back();
            ll d = seg2.query(max(k + 1, head2), N);
            if (d < N) {
                ans += r;
                seg1.update(d, N);
            }
            else {
                ans += l;
                ++head2;
            }
        }

        for (const auto [k, l, r] : vc) {
            ans += l;
        }

        cout << ans << "\n";
    }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    ofstream ofs("./in_out/output.txt");
    cout.rdbuf(ofs.rdbuf());
#endif
    solve();
    cout << flush;
    return 0;
}