#include <set>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
using namespace std;
int main() {
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        int K[n], L[n], R[n];
        vector<int> left, right;
        long long ans = 0;
        for (int i = 0; i < n; i++) {
            cin >> K[i] >> L[i] >> R[i];
            if (L[i] > R[i]) left.push_back(i);
            else right.push_back(i);
            ans += max(L[i], R[i]);
        }
        sort(left.begin(), left.end(), [&](int i, int j) { return K[i] < K[j]; });
        sort(right.begin(), right.end(), [&](int i, int j) { return K[i] > K[j]; });
        multiset<int> ld, rd;
        for (int i : left) {
            ld.insert(L[i] - R[i]);
            if (K[i] < ld.size()) {
                ans -= *ld.begin();
                ld.erase(ld.begin());
            }
        }
        for (int i : right) {
            rd.insert(R[i] - L[i]);
            if (n - K[i] < rd.size()) {
                ans -= *rd.begin();
                rd.erase(rd.begin());
            }
        }
        cout << ans << endl;
    }
}

