#include <bits/stdc++.h>
using namespace std;
using i64=int_fast64_t;

void solve()
{
    int n; cin>>n;
    struct camel
    {
        int k;
        int dif;
    };
    vector<camel> lft,rgt;
    i64 ans=0;

    for(int i=0; i<n; ++i)
    {
        int k,l,r; cin>>k>>l>>r;
        if(l>r) lft.push_back({k-1,l-r}),ans+=r;
        else rgt.push_back({k,r-l}),ans+=l;
    }
    int L=lft.size();
    vector<vector<int>> lls(n);
    for(auto c: lft)
    {
        lls[min(L-1,c.k)].push_back(c.dif);
    }
    priority_queue<int> ll;
    for(int i=L-1; i>=0; i--)
    {
        for(int d: lls[i]) ll.emplace(d);
        if(ll.empty()) continue;
        ans+=ll.top();
        ll.pop();
    }

    vector<vector<int>> rrs(n+1);
    for(auto c: rgt)
    {
        rrs[max(L,c.k)].push_back(c.dif);
    }
    priority_queue<int> rr;
    for(int i=L; i<n; i++)
    {
        for(int d: rrs[i]) rr.emplace(d);
        if(rr.empty()) continue;
        ans+=rr.top();
        rr.pop();
    }
    cout << ans << "\n";
}

int main()
{
    int T; cin>>T;
    while(T--)
    {
        solve();
    }
}