#include <bits/stdc++.h>
using namespace std;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(9), 0);
//int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv ./prog.exe") >> 8), 0);
using Int = long long;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using MaxHeap = priority_queue<T>;
Int solve(const vector<int> &K, const vector<int> &L, const vector<int> &R) {
    int N = K.size();
    Int ans = 0;
    map<int, vector<int>> M;
    for (int i = 0; i < N; i++) {
        ans += R[i];
        M[K[i]].push_back(L[i] - R[i]);
    }
    MinHeap<int> Q;
    MaxHeap<int> QQ;
    for (auto& [n, vec] : M) {
        for (auto item : vec) if (item >= 0) {
            while (Q.size() >= n && Q.top() < item) {
                Q.pop();
            }
            Q.push(item);
        }
    }
    for (auto it = rbegin(M); it != rend(M); ++it) {
        auto& [n, vec] = *it;
        int m = N - n;
        if (m == 0) continue;
        for (auto item : vec) if (item < 0) {
            while (QQ.size() >= m && QQ.top() > item) {
                QQ.pop();
            }
            QQ.push(item);
        }
    }
    while (!Q.empty()) { ans += Q.top(); Q.pop(); }
    for (auto& [n, vec] : M) for (auto item : vec) if (item < 0) ans += item;
    while (!QQ.empty()) { ans -= QQ.top(); QQ.pop(); }
    return ans;
}
int main() {
    int T; cin >> T;
    while (T--) {
        int N; cin >> N;
        vector<int> K(N), L(N), R(N);
        for (int i = 0; i < N; i++) cin >> K[i] >> L[i] >> R[i];
        cout << solve(K, L, R) << '\n';
    }
    return 0;
}