#include <bits/stdc++.h>

using ll = long long;
const int maxn = 2e5 + 3, maxlo = 0x40000;

struct SegmentTree {
    int lo;
    struct node {
        ll m, d;
        int id;
        void tag(const ll &u) {
            m += u;
            d += u;
        }
        void up(const node &l, const node &r) {
            m = std::max(l.m, r.m);
            d = 0;
            if(r.m == m)
                id = r.id;
            else
                id = l.id;
        }
        void down(node &l, node &r) {
            if(d) {
                l.tag(d);
                r.tag(d);
                d = 0;
            }
        }
    } a[maxlo * 2];
    void build(int n) {
        lo = 1;
        while(lo <= n)
            lo = lo * 2;
        for(int i = 0; i < lo; ++i)
            a[i + lo] = {-i - 1, 0, i};
        for(int i = lo - 1; i > 0; --i)
            a[i].up(a[i * 2], a[i * 2 + 1]);
    }
    void down(int k) {
        if(k < 0 || k >= lo)
            return;
        for(int i = 1, j = lo / 2; i < lo; i = i * 2 + bool(k & j), j /= 2) {
            a[i].down(a[i * 2], a[i * 2 + 1]);
        }
    }
    void up(int k) {
        if(k < 0 || k >= lo)
            return;
        for(int i = (k + lo) / 2; i > 0; i /= 2) {
            a[i].up(a[i * 2], a[i * 2 + 1]);
        }
    }
    void modify(int l, int r, const ll &v) {
        down(l - 1); down(r);
        for(int i = l + lo, j = r + lo; i < j; i /= 2, j /= 2) {
            if(i % 2)
                a[i++].tag(v);
            if(j % 2)
                a[--j].tag(v);
        }
        up(l - 1); up(r);
    }
    std::tuple<ll, int> query(int l, int r) {
        std::tuple<ll, int> ans = {std::numeric_limits<ll>::min(), -1};
        down(l - 1); down(r);
        for(int i = l + lo, j = r + lo; i < j; i /= 2, j /= 2) {
            if(i % 2) {
                ans = std::max(ans, std::make_tuple(a[i].m, a[i].id));
                i += 1;
            }
            if(j % 2) {
                j -= 1;
                ans = std::max(ans, std::make_tuple(a[j].m, a[j].id));
            }
        }
        return ans;
    }
} t;

int n, m;
struct cm {
    int k;
    ll l, r;
    bool operator<(const cm &o) const {
        return (l - r) > (o.l - o.r);
    }
    void read() {
        scanf("%d%lld%lld", &k, &l, &r);
    }
} c[maxn];

ll solveA() {
    ll ans = 0;
    t.build(m);
    for(int i = 0; i < m; ++i) {
        ll w;
        int j;
        std::tie(w, j) = t.query(0, m);
        int local;
        if(w == 0 && c[i].k <= j + 1) {
            local = m - 1;
        } else {
            local = c[i].k - 1;
            ans += c[i].l - c[i].r;
        }
        t.modify(local, m, 1);
    }
    return ans;
}

ll solveB() {
    ll ans = 0;
    int l = n - m;
    for(int i = n - 1; i >= m; --i) {
        if(c[i].k >= n) {
            ans += c[i].l - c[i].r;
            l -= 1;
        }
    }
    t.build(l);
    for(int i = n - 1; i >= m; --i) {
        if(c[i].k >= n) {
            continue;
        }
        ll w;
        int j;
        std::tie(w, j) = t.query(0, l);
        int local;
        if(w == 0 && c[i].k + 1 >= n - j) {
            local = l - 1;
            ans += c[i].l - c[i].r;
        } else {
            local = n - (c[i].k + 1);
        }
        t.modify(local, l, 1);
    }
    return ans;
}

void solve() {
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        c[i].read();
    }
    std::sort(c, c + n);
    for(m = 0; m < n; ++m) {
        if(c[m].l - c[m].r < 0)
            break;
    }
    ll ans = 0;
    for(int i = 0; i < n; ++i) {
        ans += c[i].r;
    }
    ans += solveA();
    ans += solveB();
    printf("%lld\n", ans);
}

int main() {
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; ++i)
        solve();
    return 0;
}

Measured memory_bytes: 14596