#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
typedef long long ll;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;

        ll ret = 0;
        vector<vector<ll>> wantFront, wantBack;
        priority_queue<vector<int>> queFront, queBack;
        for (int i = 0; i < n; i++) {
            int k, l, r;
            cin >> k >> l >> r;
            if (l >= r) {
                ret += r;
                wantFront.push_back({k, l - r, i});
                queFront.push({l - r, i});
            } else {
                ret += l;
                wantBack.push_back({n - k, r - l, i});
                queBack.push({l - r, i});
            }
        }

        vector<bool> used(n);

        {
            int next = 0;
            sort(wantFront.begin(), wantFront.end(), greater<>());
            for (int place = wantFront.size(); place > 0; place--) {
                while (next < wantFront.size() && used[wantFront[next][2]])next++;
                if (next >= wantFront.size())break;

                if (wantFront[next][0] >= place) {
                    ret += wantFront[next][1];
                    used[wantFront[next][2]] = true;
                    next++;
                } else {
                    vector<int> tmp;
                    do {
                        tmp = queFront.top();
                        queFront.pop();
                    } while (used[tmp[1]]);
                    used[tmp[1]] = true;
                }
            }
        }
        {
            int next = 0;
            sort(wantBack.begin(), wantBack.end(), greater<>());
            for (int place = wantBack.size(); place > 0; place--) {
                while (next < wantBack.size() && used[wantBack[next][2]])next++;
                if (next >= wantBack.size())break;

                if (wantBack[next][0] >= place) {
                    ret += wantBack[next][1];
                    used[wantBack[next][2]] = true;
                    next++;
                } else {
                    vector<int> tmp;
                    do {
                        tmp = queBack.top();
                        queBack.pop();
                    } while (used[tmp[1]]);
                    used[tmp[1]] = true;
                }
            }
        }

        cout << ret << endl;
    }
    return 0;
}