#include <bits/stdc++.h>

using namespace std;

int main() {
    int T;
    cin >> T;
    for (int n_case = 1; n_case <= T; ++n_case) {
        int n;
        cin >> n;
        long long total = 0;
        vector<pair<int, int>> left;
        vector<pair<int, int>> right;
        for (int i = 0; i < n; ++i) {
            int k, l, r;
            cin >> k >> l >> r;
            if (l > r) {
                left.emplace_back(k, l - r);
                total += r;
            } else if (r > l) {
                right.emplace_back(n - k, r - l);
                total += l;
            } else {
                total += l;
            }
        }
        sort(left.begin(), left.end());
        sort(right.begin(), right.end());
        function<void(const vector<pair<int, int>>&)> process = [&](const vector<pair<int, int>>& a) {
            multiset<int> vals;
            for (auto p: a) {
                vals.insert(p.second);
                if (vals.size() > p.first)
                    vals.erase(vals.begin());
            }
            for (int v: vals)
                total += v;
        };
        process(left);
        process(right);
        cout << total << "\n";
    }
    return 0;
}
