#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll solve(vector<pair<int,int>>& t)
{
    sort(t.begin(),t.end(),greater<pair<int,int>>());
    priority_queue<int> pq;
    ll res=0;
    for(int i=(int)t.size(),j=0;i>0;i--)
    {
        while(j<(int)t.size() && t[j].first>=i)
            pq.push(t[j++].second);
        if(!pq.empty())
            res+=pq.top(),pq.pop();
    }
    return res;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        ll base=0;
        vector<pair<int,int>> tl,tr;
        for(int i=1;i<=n;i++)
        {
            int k,l,r;
            scanf("%d%d%d",&k,&l,&r);
            if(l>r)tl.push_back({k,l-r}),base+=r;
            else tr.push_back({n-k,r-l}),base+=l;
        }
        printf("%lld\n",base+solve(tl)+solve(tr));
    }
    return 0;
}
