#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int test;
  cin >> test;
  for (int t = 0; t < (test); t++) {
    int n;
    cin >> n;
    vector<vector<pll>> L(n), R(n + 1);
    ll sum = 0;
    for (int i = 0; i < (n); i++) {
      ll k, l, r;
      cin >> k >> l >> r;
      if (k == n || l == r) {
        sum += l;
      } else if (l > r) {
        L[k - 1].push_back({l - r, l});
      } else if (l < r) {
        R[k].push_back({r - l, r});
      }
    }
    priority_queue<pll, vector<pll>> que;
    for (int i = 0; i < (n + 1); i++) {
      for (auto a : R[i]) que.push(a);
      if (!que.empty()) {
        auto tmp = que.top();
        que.pop();
        sum += tmp.second;
      }
    }
    while (!que.empty()) {
      auto tmp = que.top();
      que.pop();
      sum += tmp.second - tmp.first;
    }
    for (int i = 0; i < (n); i++) {
      for (auto a : L[i]) que.push(a);
      if (!que.empty()) {
        auto tmp = que.top();
        que.pop();
        sum += tmp.second;
      }
    }
    while (!que.empty()) {
      auto tmp = que.top();
      que.pop();
      sum += tmp.second - tmp.first;
    }
    cout << sum << ln;
  }
}
