#include <bits/stdc++.h>
using namespace std;
const auto start_time = std::chrono::high_resolution_clock::now();
void timecalculater() {}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char *x) { cout << '\"' << x << '\"'; }
void __print(const string &x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x) cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cout << ", ";
  _print(v...);
}
long long p = 1e9 + 7;
const int N = 2e5 + 5;
struct node {
  long long k, l, r;
};
bool comp1(node x, node y) {
  if (x.l == y.l) {
    return x.k < y.k;
  }
  return x.l > y.l;
}
bool comp2(node x, node y) {
  if ((x.r - x.l) == (y.r - y.l)) {
    return x.k > y.k;
  }
  return (x.r - x.l) > (y.r - y.l);
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  ;
  int T = 1;
  cin >> T;
  while (T--) {
    long long n;
    cin >> n;
    node a[n];
    vector<node> left, right;
    for (int i = 0; i < (n); ++i) {
      long long k, l, r;
      cin >> k >> l >> r;
      a[i] = {k, l, r};
      if (l > r) {
        left.push_back({k, l, r});
      } else {
        right.push_back({k, l, r});
      }
    }
    sort(left.begin(), left.end(), comp1);
    long long ans = 0;
    set<int> s;
    for (int i = 0; i < (n); ++i) s.insert(i + 1);
    for (int i = 0; i < ((int)(left).size()); ++i) {
      auto j = s.upper_bound(left[i].k);
      if (j != s.begin()) {
        ans += left[i].l;
        j--;
        s.erase(j);
      } else {
        ans += left[i].r;
      }
    }
    s.clear();
    for (int j = (int)(left).size() + 1; j <= n; j++) {
      s.insert(j);
    }
    sort(right.begin(), right.end(), comp2);
    for (int i = 0; i < ((int)(right).size()); ++i) {
      auto j = s.upper_bound(right[i].k);
      if (j != s.end()) {
        ans += right[i].r;
        s.erase(j);
      } else {
        ans += right[i].l;
      }
    }
    cout << ans << '\n';
  }
  timecalculater();
  return 0;
}
