#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<array>

using namespace std;

bool my_greater(int lhs[3], int rhs[3]){
    if(max(lhs[1],lhs[2]) != max(rhs[1],rhs[2])){
        return max(lhs[1],lhs[2]) > max(rhs[1],rhs[2]);
    }else{
        return true;
    }
}

int main(){
    long long int T;
    cin >> T;
    for(long long int i = 0; i < T; i++){
        long long int N;
        cin >> N;
        vector<array<int, 3>> v;
        for(int j = 0; j < N; j++){
            long long int K, L, R;
            cin >> K >> L >> R;
            array<int, 3> t;
            t[0] = K, t[1] = L,t[2] = R;
            v.push_back(t); 
        }
        sort(v.begin(), v.end());
        bool seat[N] ={false};
        long long int p = 0;
        for(int i = 0; i < N; i++){
            int k = v[i][0];
            k--;
            bool isback = v[i][1] < v[i][2] ? true : false;
            long long int gold = max(v[i][1], v[i][2]);
            long long int silver = min(v[i][1], v[i][2]);
            if(isback){
                bool f = true;
                for(int j = k+1; j < N; j++){
                    if(!seat[j]){
                        seat[j] = true;
                        p += gold;
                        f = false;
                        break;
                    }
                }
                if(f){
                    p += silver;
                }
            }else{
                bool f = true;
                for(int j = k; j >= 0; j--){
                    if(!seat[j]){
                        seat[j] = true;
                        p += gold;
                        f = false;
                        break;
                    }
                }
                if(f){
                    p += silver;
                }
            }
        }
        cout << p << endl;
    }
}