#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
vector<long long> g[2][N];
void testcase() {
  long long n;
  cin >> n;
  long long ans = 0;
  for (long long i = 0; i < n; ++i) {
    long long k, l, r;
    cin >> k >> l >> r;
    if (l >= r) {
      g[0][k].push_back(l - r);
    } else {
      g[1][n - k].push_back({r - l});
    }
    ans += min(l, r);
  }
  for (long long u : {0, 1}) {
    multiset<long long> now;
    for (long long k = 1; k <= n; ++k) {
      for (long long &profit : g[u][k]) {
        now.insert(profit);
        ans += profit;
      }
      while ((long long)now.size() > k) {
        ans -= *now.begin();
        now.erase(*now.begin());
      }
    }
  }
  cout << ans << '\n';
  for (long long i = 1; i <= n; ++i) {
    g[0][i].clear(), g[1][i].clear();
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    testcase();
  }
  return 0;
}
