#include <bits/stdc++.h>
using namespace std;using ll = long long int;int main(){int T,N,k;ll o,l,r;cin>>T;while(T){T--;o=0;cin>>N;vector<ll> P[N],S[N];priority_queue<ll, vector<ll>, greater<ll>> H,T;for(int n=0;n<N;n++){cin>>k>>l>>r;if(l>r){P[k-1].push_back(l-r);o+=r;}else{S[k-1].push_back(r-l);o+=l;}}for(int i=0;i<N;i++){for(int j:P[i])H.push(j);while(H.size()>i+1)H.pop();for(int j:S[N-1-i])T.push(j);while(T.size()>i)T.pop();}while(!H.empty()){o=o+H.top();H.pop();}while(!T.empty()){o=o+T.top();T.pop();}cout<<o<<endl;}}
