#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
int L[MAX], R[MAX], K[MAX];
int n;
vector<int> find_order(vector<int> v) {
  sort(begin(v), end(v),
       [](int lhs, int rhs) { return L[lhs] - R[lhs] > L[rhs] - R[rhs]; });
  vector<int> order(v.size());
  set<int> pos;
  for (int i = int(0); i < int(order.size()); i++) pos.insert(i);
  for (auto &each : v) {
    int want = min(K[each] - 1, (int)v.size() - 1);
    auto it = pos.upper_bound(want);
    if (it != pos.begin()) {
      it = prev(it);
    }
    order[*it] = each;
    pos.erase(it);
  }
  return order;
}
void solve() {
  cin >> n;
  vector<int> wL, wR;
  for (int i = int(1); i < int(n + 1); i++) {
    cin >> K[i] >> L[i] >> R[i];
    if (L[i] > R[i])
      wL.push_back(i);
    else {
      wR.push_back(i);
      swap(L[i], R[i]);
      K[i] = n - K[i];
    }
  }
  vector<int> L_order = find_order(wL);
  vector<int> R_order = find_order(wR);
  reverse(begin(R_order), end(R_order));
  for (auto &each : R_order) {
    swap(L[each], R[each]);
    K[each] = n - K[each];
  }
  vector<int> final_order;
  for (auto &each : L_order) final_order.push_back(each);
  for (auto &each : R_order) final_order.push_back(each);
  long long ans = 0;
  for (int i = int(0); i < int(final_order.size()); i++) {
    int id = final_order[i];
    if (i + 1 <= K[id])
      ans += L[id];
    else
      ans += R[id];
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = int(0); i < int(t); i++) solve();
  return 0;
}
