#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 5e5 + 20;
int T;
int k[MAXN], l[MAXN], r[MAXN];
vector<pair<int, int> > v;
vector<int> used;
set<int> unused;
set<int>::iterator ite;
void solve() {
  v.clear();
  unused.clear();
  used.clear();
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> k[i] >> l[i] >> r[i];
  }
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    res += r[i];
    if (r[i] != l[i]) v.push_back(make_pair(l[i] - r[i], k[i]));
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  if (v.empty()) {
    cout << res << endl;
    return;
  }
  for (int i = 1; i <= n; ++i) unused.insert(i);
  int notsure = n - v.size();
  n = v.size();
  for (int i = 0; i < n; ++i) {
    if (v[i].first > 0) {
      ite = unused.upper_bound(v[i].second);
      if (ite != unused.begin()) {
        ite--;
        used.push_back(*ite);
        res += v[i].first;
        unused.erase(ite);
      } else
        notsure++;
    } else {
      break;
    }
  }
  sort(used.begin(), used.end());
  for (int i = 0; i < used.size(); ++i) {
    if (!unused.empty())
      if (used[i] > *(unused.begin())) {
        unused.insert(used[i]);
        unused.erase(unused.begin());
      }
  }
  for (int i = 1; i <= notsure; ++i) {
    unused.erase(unused.begin());
  }
  for (int i = n - 1; i >= 0; --i) {
    if (v[i].first < 0) {
      ite = unused.upper_bound(v[i].second);
      if (ite != unused.end()) {
        unused.erase(ite);
      } else {
        unused.erase(unused.begin());
        res += v[i].first;
      }
    }
  }
  cout << res << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> T;
  while (T--) solve();
  return 0;
}
