#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
vector<int> v[1 + N], v2[1 + N];
int k[1 + N], l[1 + N], r[1 + N];
bool used[1 + N];
void solveTest() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) v[i].clear(), used[i] = false, v2[i].clear();
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> k[i] >> l[i] >> r[i];
    ans += min(r[i], l[i]);
    if (l[i] > r[i])
      v[k[i]].push_back(i);
    else {
      swap(l[i], r[i]);
      v2[k[i] + 1].push_back(i);
    }
  }
  multiset<pair<int, int>> active;
  for (int i = 1; i <= n; i++) {
    for (int x : v2[i]) active.insert({l[x] - r[x], x});
    if (active.size()) {
      used[i] = true;
      auto it = prev(active.end());
      ans += it->first;
      active.erase(it);
    }
  }
  active.clear();
  for (int i = n; i > 0; i--) {
    for (int x : v[i]) active.insert({l[x] - r[x], x});
    if (active.size() && used[i] == false) {
      auto it = prev(active.end());
      ans += it->first;
      active.erase(it);
    }
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solveTest();
  return 0;
}
