#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) {
  cout << (expr ? "Yes" : "No") << "\n";
}
struct Camel {
    int K;
    lint L;
    lint R;
};
struct Segment_Sum {
private:
  int n;
  vector<int> node;
public:
  Segment_Sum(int sz) {
    n = 1; while(n < sz) n *= 2;
    node.resize(2*n-1, 0);
    for(int i=n-2; i>=0; i--) node[i] = node[2*i+1] + node[2*i+2];
  }
  void update(int x) {
    x += (n - 1);
    node[x]++;
    while(x > 0) {
      x = (x - 1) / 2;
      node[x] = node[2*x+1] + node[2*x+2];
    }
  }
  int getsum(int a, int b, int k=0, int l=0, int r=-1) {
    if(r < 0) r = n;
    if(r <= a || b <= l) return 0;
    if(a <= l && r <= b) return node[k];
    int vl = getsum(a, b, 2*k+1, l, (l+r)/2);
    int vr = getsum(a, b, 2*k+2, (l+r)/2, r);
    return vl + vr;
  }
};
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    REP(t, T) {
        int N;
        cin >> N;
        vector<Camel> camels;
        REP(i, N) {
            camels.emplace_back();
            cin >> camels[i].K >> camels[i].L >> camels[i].R;
            camels[i].K--;
        }
        sort(ALL(camels), [](auto const &a, auto const &b) { return abs(a.L-a.R) > abs(b.L-b.R); });
        lint ans = 0;
        Segment_Sum lseg(N);
        Segment_Sum rseg(N);
        REP(i, N) {
            if(camels[i].L >= camels[i].R) {
                if(lseg.getsum(0, camels[i].K+1) < camels[i].K+1) {
                    ans += camels[i].L;
                    lseg.update(camels[i].K);
                } else {
                    ans += camels[i].R;
                    rseg.update(camels[i].K);
                }
            } else {
                if(rseg.getsum(camels[i].K+1, N) < N - camels[i].K - 1) {
                    ans += camels[i].R;
                    rseg.update(camels[i].K);
                } else {
                    ans += camels[i].L;
                    lseg.update(camels[i].K);
                }
            }
        }
        cout << ans << "\n";
    }
}