#include <iostream>
#include <algorithm>
#include <array>
#include <utility>
#include <vector>
#include <tuple>
#include <queue>

constexpr int MAX_N = 200'000;
std::array<std::array<int, 2>, MAX_N + 1> L;
std::array<std::array<int, 2>, MAX_N + 1> R;

void solve(void){
    int n; std::cin >> n;
    long long int sum = 0;
    int len_l = 0, len_r = 0;
    for(int i = 0; i < n; ++i){
        int a, l, r; std::cin >> a >> l >> r;
        if(l > r){ L[len_l++] = {a, l - r}; sum += r; }
        else if(l < r){ R[len_r++] = {n - a, r - l}; sum += l; }
        else sum += l;
    }
    std::sort(L.begin(), L.begin() + len_l);
    std::sort(R.begin(), R.begin() + len_r);
    std::priority_queue<int, std::vector<int>, std::greater<int>> PQ;

    long long int addr = 0;
    for(int x = 0, i = 0; x <= len_l; ++x){
        if(x == len_l) for(; i < len_l; ++i) addr += L[i][1];
        else while(i < len_l and L[i][0] == x){ PQ.push(L[i][1]); addr += L[i][1]; ++i;  }
        while(PQ.size() > x){ addr -= PQ.top(); PQ.pop(); }
    }
    while(not PQ.empty()) PQ.pop();

    for(int x = 0, i = 0; x <= len_r; ++x){
        if(x == len_r) for(; i < len_l; ++i) addr += R[i][1];
        else while(i < len_r and R[i][0] == x){ PQ.push(R[i][1]); addr += R[i][1]; ++i; }
        while(PQ.size() > x ){ addr -= PQ.top(); PQ.pop(); }
    }
    std::cout << (addr + sum) << '\n';
}


int main(void){
    std::cin.tie(nullptr); std::cin.sync_with_stdio(false);
    int Q; std::cin >> Q;
    while(Q--) solve();
    return 0;
}