#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}
const ll INF = 1LL << 60;
ll happy(vector<P> c) {
  int cn = c.size();
  for (int i = 0; i < (cn); ++i) c[i].first = min(c[i].first, cn);
  vector<vector<int>> carray(cn + 1);
  for (int i = 0; i < (cn); ++i) carray[c[i].first].push_back(c[i].second);
  vector<int> ok;
  for (int i = 1; i <= cn; ++i) {
    for (int x : carray[i]) ok.push_back(x);
    sort(ok.begin(), ok.end(), greater<int>());
    while (ok.size() > i) ok.pop_back();
  }
  ll h = 0;
  for (int x : ok) h += x;
  return h;
}
void solve() {
  int N;
  cin >> N;
  vector<P> mae, ato;
  int Ki, Li, Ri;
  ll ans = 0;
  for (int i = 0; i < (N); ++i) {
    cin >> Ki >> Li >> Ri;
    if (Li > Ri) {
      mae.emplace_back(Ki, Li - Ri);
    } else {
      ato.emplace_back(N - Ki, Ri - Li);
    }
    ans += min(Li, Ri);
  }
  ans += happy(mae);
  ans += happy(ato);
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  for (int ti = 0; ti < (T); ++ti) solve();
  return 0;
}
