#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long linf = 1LL << 62;
const int MAX = 510000;
long long dy[8] = {0, -1, 0, 1, 1, -1, 1, -1};
long long dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T>
inline void print(T &a) {
  for (int i = 0; i < (a.size()); i++) cout << a[i] << " ";
  cout << "\n";
}
template <typename T1, typename T2>
inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\n";
}
const long long mod = 1e9 + 7;
struct dat {
  long long k, l, r, lr, id;
  bool operator<(const dat &d) const { return lr < d.lr; }
};
void solve() {
  long long n;
  cin >> n;
  vector<dat> v(n);
  for (int i = 0; i < (n); i++) {
    long long k, l, r;
    cin >> k >> l >> r;
    k--;
    v[i] = {k, l, r, r - l, i};
  }
  sort((v).begin(), (v).end());
  set<int> se;
  for (int i = 0; i < (n); i++) se.insert(i);
  vector<bool> visit(n, false);
  long long ans = 0;
  int sz = 0;
  for (int i = 0; i < (n); i++) {
    if (v[i].lr > 0) break;
    visit[v[i].id] = true;
    auto itr = se.upper_bound(v[i].k);
    if (itr == se.begin())
      ans += v[i].l;
    else {
      ans += v[i].l;
      itr--;
      se.erase(itr);
    }
    sz++;
  }
  se.clear();
  for (int i = sz; i < n; i++) se.insert(i);
  for (int i = n - 1; i >= 0; i--)
    if (!visit[v[i].id]) {
      auto itr = se.upper_bound(v[i].k);
      if (itr == se.end()) {
        ans += v[i].l;
        se.erase(se.begin());
      } else {
        ans += v[i].r;
        se.erase(itr);
      }
    }
  cout << ans << "\n";
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
