#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
long long TT;
void solve(long long t);
void answer() {
  cin >> TT;
  for (long long t = 1; t <= (TT); t++) solve(t);
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  answer();
  return 0;
}
long long N, Q, K;
long long A[223450];
struct Item {
  long long k, l, r;
  bool operator<(const Item &that) const {
    return abs(l - r) < abs(that.l - that.r);
  }
};
void solve(long long _t) {
  cin >> N;
  long long K, L, R;
  vector<Item> items;
  priority_queue<Item> que;
  set<long long> s;
  vector<vector<long long>> lefts(N), rights(N);
  long long ans = 0;
  for (long long i = 0; i < (N); i++) {
    cin >> K >> L >> R;
    --K;
    items.push_back({K, L, R});
    if (L > R) {
      lefts[K].push_back(i);
    }
    if (R > L) {
      rights[K].push_back(i);
    }
    if (L == R) {
      ans += L;
    }
  }
  long long pos = 0;
  for (long long i = 0; i < (N); i++) {
    for (long long j : lefts[i]) que.push(items[j]);
    while (que.size() && pos <= i) {
      auto [k, l, r] = que.top();
      que.pop();
      if (pos <= k) {
        ans += l;
        pos++;
      } else {
        ans += r;
      }
    }
  }
  pos = N - 1;
  for (long long i = (N - 1); i >= 0; i--) {
    for (long long j : rights[i]) que.push(items[j]);
    while (que.size() && pos >= i) {
      auto [k, l, r] = que.top();
      que.pop();
      if (pos <= k) {
        ans += l;
      } else {
        ans += r;
        pos--;
      }
    }
  }
  printf("%lld\n", ans);
}
