#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ii = pair<int, int>;
using vii = vector<ii>;
using ll = long long;
string to_string(string s) { return '\"' + s + '\"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename T>
string to_string(T v) {
  long long f = 0;
  string res = "{";
  for (const auto& x : v) res += (f++ ? ", " : "") + to_string(x);
  return res + "}";
}
void debug_out() { cerr << "]\n"; }
template <typename T, typename... V>
void debug_out(T a, V... v) {
  cerr << to_string(a);
  if (sizeof...(v)) cerr << ", ";
  debug_out(v...);
}
priority_queue<tuple<long long, long long, long long>> pqL, pqR;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    while (!pqL.empty()) pqL.pop();
    while (!pqR.empty()) pqR.pop();
    long long n;
    cin >> n;
    ll ans = 0;
    for (auto(i) = (0); (i) <= (n - 1); (i)++) {
      long long k, l, r;
      cin >> k >> l >> r;
      if (l == r) ans += l;
      if (l > r) pqL.emplace(-(k - 1), l - r, r);
      if (l < r) pqR.emplace(k, r - l, l);
    }
    long long i = -1;
    while (!pqL.empty()) {
      long long k, diff, add;
      tie(k, diff, add) = pqL.top();
      pqL.pop();
      k = -k;
      if (k <= i)
        ans += add;
      else {
        ans += diff + add;
        i++;
      }
    }
    long long j = n;
    while (!pqR.empty()) {
      long long k, diff, add;
      tie(k, diff, add) = pqR.top();
      pqR.pop();
      if (k >= j)
        ans += add;
      else {
        ans += diff + add;
        j--;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
