#include <bits/stdc++.h>
using namespace std;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)
 
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
 
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
 
const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;
 
// -------------------------------------------------------
 
ll T;
 
vector<ll> ansvec;
 
int main() {
  cin >> T;
  rep(i, 0, T) {
    ll N;
    cin >> N;
    map<ll, vector<Pll>> red, blue;
    priority_queue<ll, vector<ll>, greater<ll>> red_que, blue_que;
    ll ans = 0;
    rep(j, 0, N) {
      ll k, l, r;
      cin >> k >> l >> r;
      ans += min(l, r);
      if (l >= r) {
        red[k].push_back(Pll(l, r));
      } else {
        blue[N - k].push_back(Pll(r, l));
      }
    }
 
    repe(i, 1, N) {
      for (auto e : red[i]) {
        red_que.push(e.first - e.second);
      }
      while (red_que.size() > i) {
        red_que.pop();
      }
    }
    while (!red_que.empty()) {
      auto add = red_que.top();
      red_que.pop();
      ans += add;
    }
 
    repe(i, 1, N) {
      for (auto e : blue[i]) {
        blue_que.push(e.first - e.second);
      }
      while (blue_que.size() > i) {
        blue_que.pop();
      }
    }
    while (!blue_que.empty()) {
      auto add = blue_que.top();
      blue_que.pop();
      ans += add;
    }
 
    ansvec.push_back(ans);
  }
 
  for (auto e : ansvec) {
    cout << e << "\n";
  }
}