#include <bits/stdc++.h>
using Int = long long;  // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Tue Jul 21 18:31:19 JST 2020
 **/

void solve() {
    Int n;
    std::cin >> n;
    std::vector<std::vector<Int>> bl(n), br(n);
    Int ans = 0;
    REP(i, n) {
        Int k, l, r;
        std::cin >> k >> l >> r, k--;
        ans += std::min(l, r);
        if (l > r) {
            bl[k].push_back(l - r);
        } else {
            if (n - 2 - k >= 0) br[n - 2 - k].push_back(r - l);
        }
    }
    for (auto& v : {bl, br}) {
        std::priority_queue<Int> pq;
        for (Int i = n - 1; i >= 0; i--) {
            for (Int x : v[i]) pq.push(x);
            if (pq.size()) ans += pq.top(), pq.pop();
        }
    }
    std::cout << ans << "\n";
}
signed main() {
    Int T;
    std::cin >> T;
    while (T--) solve();
}
