#include <bits/stdc++.h>
using namespace std;
template <typename T>
using pq_asc = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
ostream &JOUT(T s, T e, string sep = " ", ostream &os = cout) {
  if (s != e) {
    os << *s;
    ++s;
  }
  while (s != e) {
    os << sep << *s;
    ++s;
  }
  return os;
}
ostream &YES(bool cond, string yes = "Yes", string no = "No",
             ostream &os = cout) {
  if (cond) {
    os << yes << endl;
  } else {
    os << no << endl;
  }
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  JOUT((v).begin(), (v).end(), ", ", os) << ']';
  return os;
}
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
template <typename T>
struct bit {
  long long n;
  vector<T> a;
  bit() {}
  bit(long long n) : n(n) { a.assign(n + 1, 0); }
  void add(long long i, T x) {
    ++i;
    while (i <= n) {
      a[i] += x;
      i += i & -i;
    }
  }
  T sum(long long i) {
    T s = 0;
    while (i > 0) {
      s += a[i];
      i -= i & -i;
    }
    return s;
  }
};
int main() {
  long long T;
  cin >> T;
  ;
  for (long long t = 0; t < (T); ++t) {
    long long N;
    cin >> N;
    ;
    vector<long long> K(N), L(N), R(N);
    for (long long i = 0; i < (N); ++i) {
      cin >> K[i] >> L[i] >> R[i];
    }
    long long ans = 0;
    long long p = 0;
    long long q = 0;
    vector<pair<long long, long long>> P, Q;
    for (long long i = 0; i < (N); ++i) {
      ans += min(L[i], R[i]);
      if (L[i] > R[i]) {
        ++p;
      } else if (L[i] < R[i] and K[i] < N) {
        ++q;
      }
    }
    for (long long i = 0; i < (N); ++i) {
      if (L[i] > R[i]) {
        P.push_back({L[i] - R[i], min(p, K[i])});
      } else if (L[i] < R[i] and K[i] < N) {
        Q.push_back({R[i] - L[i], min(q, N - K[i])});
      }
    }
    sort((P).begin(), (P).end());
    reverse((P).begin(), (P).end());
    sort((Q).begin(), (Q).end());
    reverse((Q).begin(), (Q).end());
    bit<long long> Cp(P.size() + 2);
    bit<long long> Cq(Q.size() + 2);
    for (auto [d, k] : P) {
      if (Cp.sum(k + 1) < k) {
        Cp.add(k, 1);
        ans += d;
      }
    }
    for (auto [d, k] : Q) {
      if (Cq.sum(k + 1) < k) {
        Cq.add(k, 1);
        ans += d;
      }
    }
    cout << (ans) << endl;
    ;
  }
}
