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

void solve () {
    int n;
    std::cin >> n;
    std::vector<int> k(n), l(n), r(n), a, b;
    for (int i = 0; i < n; i++) {
        std::cin >> k[i] >> l[i] >> r[i];
    }

    long long ans = 0;
    std::vector<int> index[2];
    for (int i = 0; i < n; i++) {
        ans += std::min(l[i], r[i]);
        if (l[i] >= r[i]) {
            index[0].push_back(i);
        } else {
            index[1].push_back(i);
            std::swap(l[i], r[i]);
            k[i] = n - k[i];
        }
    }
    
    auto calc = [&] (const std::vector<int> &v) -> void {
        std::vector<int> idx[n + 1];
        for (int i : v) idx[k[i]].push_back(i);
        std::priority_queue<int, std::vector<int>, std::greater<int>> que;
        for (int j = 0; j < n + 1; j++) {
            for (int i : idx[j]) que.push(l[i] - r[i]);
            while (que.size() > j) que.pop();
        }
        while (not que.empty()) {
            ans += que.top();
            que.pop();
        }
    };

    for (int i = 0; i < 2; i++) calc(index[i]);

    std::cout << ans << '\n';

    return;
}

int main() {
    std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
    int t;
    std::cin >> t;
    while (t--) solve();
    return 0;
}