#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        vector<vector<vector<int>>> f(2, vector<vector<int>>(n + 1));
        ll sum = 0;
        for (int i = 0; i < n; ++i) {
            int k, l, r; cin >> k >> l >> r;
            if (l >= r) {
                f[0][k].emplace_back(r - l);
                sum += l;
            }
            else {
                if (n - k == 0) {
                    sum += l;
                    continue;
                }
                f[1][n - k].emplace_back(l - r);
                sum += r;
            }
        }
        for (int j = 0; j < 2; ++j) {
            priority_queue<int> pq;
            for (int i = 1; i <= n; ++i) {
                for (auto p : f[j][i]) pq.emplace(p);
                while (pq.size() > i) {
                    sum += pq.top();
                    pq.pop();
                }
            }
        }
        cout << sum << '\n';
    }
}