#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
 
#define $(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\n'
#define x first
#define y second
#define int long long
#define double long double
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define beg begin
#define dbg(x) cout << #x << " = " << x << endl;
 
template<class T> ostream& operator<<(ostream &str, vector<T> &a) {
    for (auto &i : a) {
        str << i << " ";
    }
    return str;
}
 
template<class T> istream& operator>>(istream &str, vector<T> &a) {
    for (auto &i : a) {
        str >> i;
    }
    return str;
}
 
template<class T> ostream& operator<<(ostream &str, pair<T, T> &a) {
    str << a.first << " " << a.second;
    return str;
}
 
template<class T> istream& operator>>(istream &str, pair<T, T> &a) {
    str >> a.first >> a.second;
    return str;
}
 
void solve(); signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    srand(time(0));
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout.setf(ios::fixed); cout.precision(6);
    solve();    
    return 0;   
}
 
const int MOD = 1e9 + 7, MAXN = 5e5 + 10, INF = 2e9 + 10, BASE = 37; 

struct node {
    int l, r, val;
} t[MAXN * 4];

void build(int v, int tl, int tr) {
    if (tl == tr) {
        t[v].l = tl, t[v].r = tl, t[v].val = 0;
    }
    else {
        int tm = (tl + tr) / 2;
        build(v * 2, tl, tm);
        build(v * 2 + 1, tm + 1, tr);
        t[v].l = min(t[v * 2].l, t[v * 2 + 1].l);
        t[v].r = max(t[v * 2].r, t[v * 2 + 1].r);
    }
}

void upd(int v, int tl, int tr, int id) {
    if (tr < id || tl > id) {
        return;
    }
    if (tl == tr) {
        t[v].val = 1;
        t[v].l = INF, t[v].r = -INF;
        return;
    }
    int tm = (tl + tr) / 2;
    upd(v * 2, tl, tm, id);
    upd(v * 2 + 1, tm + 1, tr, id);
    t[v].l = min(t[v * 2].l, t[v * 2 + 1].l);
    t[v].r = max(t[v * 2].r, t[v * 2 + 1].r);
}

pii query(int v, int tl, int tr, int l, int r) {
    if (tr < l || tl > r) {
        return {INF, -INF};
    }
    if (l <= tl && tr <= r) {
        return {t[v].l, t[v].r};
    }
    int tm = (tl + tr) / 2;
    pii ans1 = query(v * 2, tl, tm, l, r);
    pii ans2 = query(v * 2 + 1, tm + 1, tr, l, r);

    pii ans = {INF, -INF};
    ans.x = min(ans1.x, ans2.x);
    ans.y = max(ans1.y, ans2.y);
    return ans;
}

void ss() {
    int n;
    vec<vec<int>> a;
    cin >> n;
    int ans = 0;
    int cnt1 = 0, cnt2 = 0;
    for (int i = 0; i < n; ++i) {
        int k, l, r;
        cin >> k >> l >> r;
        ans += max(l, r);
        if (l - r >= 0) {
            a.pb({l - r, k, 1});
            cnt1++;
        }
        else {
            a.pb({r - l, k, -1});
            cnt2++;
        }
    }

    sort(rall(a));

    build(1, 1, n);

    for (auto it : a) {
        if (it[2] == 1) {
            pii cur = query(1, 1, n, 1, min(cnt1, it[1]));
            if (cur.x == INF) {
                ans -= it[0];
            }
            else {
                upd(1, 1, n, cur.y);
            }
        }
        else {
            pii cur = query(1, 1, n, max(it[1] + 1,n - cnt2 + 1), n);
            if (cur.x == INF) {
                ans -= it[0];
            }
            else {
                upd(1, 1, n, cur.x);
            }
        }
    }

    cout << ans << endl;
}

void solve() {
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        ss();
    }
}

Measured memory_bytes: 26384