#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct P {
    bool operator<(const P &p) const {
        return g > p.g;
    }
    int k, g;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int T;
    cin >> T;

    while (T--) {
        int n;
        cin >> n;

        ll s = 0;
        vector<P> p[2];
        for (int i = 0; i < n; i++) {
            int k, l, r;
            cin >> k >> l >> r;

            s += min(l, r);
            int g = l - r;
            p[g < 0].push_back({ g < 0 ? n - k : k, abs(g) });
        }
        for (int h = 0; h < 2; h++) {
            sort(p[h].begin(), p[h].end());
            int m = p[h].size();
            set<int> st;
            for (int i = 0; i < m; i++) {
                st.insert(st.end(), i);
            }
            for (int i = 0; i < m; i++) {
                auto it = st.lower_bound(p[h][i].k);
                if (it == st.begin()) continue;
                st.erase(--it);
                s += p[h][i].g;
            }
        }
        cout << s << '\n';
    }

    return 0;
}