#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007

using namespace std;
typedef long long ll;

mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

template <typename T> void read(T &x) {
    x = 0;
    char c = getchar();
    T sig = 1;
    for (; !isdigit(c); c = getchar())
        if (c == '-')
            sig = -1;
    for (; isdigit(c); c = getchar())
        x = (x << 3) + (x << 1) + c - '0';
    x *= sig;
}

class Solution {
public:
    void solve() {
        int n;
        read(n);
        vector<vector<pair<int, int>>> v(n + 1);
        ll ans = 0;
        for (int i = 0; i < n; ++i) {
            int k, l, r;
            read(k), read(l), read(r);
            v[k].emplace_back(l, r);
            ans += r;
        }
        priority_queue<pair<int, int>> pq;
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> tmp;
        vector<bool> vis(n + 1);
        for (int i = n; i >= 1; --i) {
            for (auto p : v[i])
                pq.push({p.first - p.second, i});
            if (!pq.empty() && pq.top().first > 0) {
                ans += pq.top().first;
                pq.pop();
                vis[i] = true;
            }
        }
        while (!pq.empty()) {
            tmp.push(pq.top());
            pq.pop();
        }
        int cnt = 0;
        for (int i : vis)
            cnt += i;
        vis.assign(n + 1, false);
        for (int i = 1; i <= cnt; ++i)
            vis[i] = true;
        int curr = n;
        while (!tmp.empty()) {
            while (curr >= 1 && vis[curr])
                curr--;
            if (curr <= tmp.top().second)
                ans += tmp.top().first;
            vis[curr] = true;
            tmp.pop();
        }
        cout << ans << endl;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    read(t);
    while (t--) {
        Solution solution = Solution();
        solution.solve();
    }
}