#include <bits/stdc++.h>
using Int = long long;
struct SetupIO {
  SetupIO() {
    std::cin.tie(nullptr), std::ios::sync_with_stdio(false),
        std::cout << std::fixed << std::setprecision(13);
  }
} setup_io;
void solve() {
  Int n;
  std::cin >> n;
  std::vector<std::vector<Int>> bl(n), br(n);
  Int ans = 0;
  for (Int i = (0), limi = (n); i < limi; i++) {
    Int k, l, r;
    std::cin >> k >> l >> r, k--;
    ans += std::min(l, r);
    if (l > r) {
      bl[k].push_back(l - r);
    } else {
      if (n - 2 - k >= 0) br[n - 2 - k].push_back(r - l);
    }
  }
  for (auto& v : {bl, br}) {
    std::priority_queue<Int> pq;
    for (Int i = (0), limi = (n); i < limi; i++) {
      for (Int x : v[i]) pq.push(x);
      while (pq.size() > i + 1) pq.pop();
    }
    while (pq.size()) ans += pq.top(), pq.pop();
  }
  std::cout << ans << "\n";
}
signed main() {
  Int T;
  std::cin >> T;
  while (T--) solve();
}
