#include <bits/stdc++.h>
template <typename T>
using MaxHeap = std::priority_queue<T>;
template <typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <typename T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
void vdeb(std::vector<T> &da) {
  for (int i = 0; i < da.size(); i++) {
    if (i == da.size() - 1)
      std::cout << da[i];
    else
      std::cout << da[i] << ' ';
  }
  std::cout << '\n';
}
template <typename T>
void vdeb(std::vector<std::vector<T>> &da) {
  for (int i = 0; i < da.size(); i++) {
    std::cout << i << ' ';
    vdeb(da[i]);
  }
  std::cout << '\n';
}
using namespace std;
long long solve() {
  int n;
  cin >> n;
  vector<vector<int>> da(n, vector<int>(0));
  vector<std::pair<long long, long long>> score(n);
  for (int i = (0); i < (int)(n); i++) {
    int k;
    cin >> k >> score[i].first >> score[i].second;
    da[k - 1].push_back(i);
  }
  long long ret = 0;
  for (int i = (0); i < (int)(n); i++)
    ret += min(score[i].first, score[i].second);
  MinHeap<long long> pq;
  for (int i = (0); i < (int)(n); i++) {
    for (int j = (0); j < (int)(da[i].size()); j++) {
      if (score[da[i][j]].first > score[da[i][j]].second) {
        pq.push(score[da[i][j]].first - score[da[i][j]].second);
      }
    }
    while (pq.size() > i + 1) {
      pq.pop();
    }
  }
  while (!pq.empty()) {
    ret += pq.top();
    pq.pop();
  }
  for (int i = (1); i < (int)(n); i++) {
    for (int j = (0); j < (int)(da[n - i - 1].size()); j++) {
      if (score[da[n - i - 1][j]].first < score[da[n - i - 1][j]].second) {
        pq.push(score[da[n - i - 1][j]].second - score[da[n - i - 1][j]].first);
      }
    }
    while (pq.size() > i + 1) {
      pq.pop();
    }
  }
  while (!pq.empty()) {
    ret += pq.top();
    pq.pop();
  }
  return ret;
}
int main() {
  int t;
  cin >> t;
  vector<long long> ans(t);
  for (int i = (0); i < (int)(t); i++) ans[i] = solve();
  for (int i = (0); i < (int)(t); i++) printf("%lld\n", ans[i]);
}
