#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class c>
  typename enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }
  template <class c, class b>
  debug& operator<<(pair<b, c> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  template <class c>
  debug& operator<<(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
};
vector<int> ft0;
vector<int> ft1;
int n;
void increment(int idx, int which) {
  idx++;
  for (int i = idx; i <= n; i += (i & (-i))) {
    if (which == 0)
      ft0[i]++;
    else
      ft1[i]++;
  }
}
long long get_sum(int from, int which) {
  int ret = 0;
  for (int i = from; i > 0; i -= (i & (-i))) {
    if (which == 0)
      ret += ft0[i];
    else
      ret += ft1[i];
  }
  return ret;
}
long long get_sum(int from, int to, int which) {
  if (from > to) return 0;
  return get_sum(to + 1, which) - get_sum(from, which);
}
int main() {
  ios::sync_with_stdio(false);
  int tc;
  cin >> tc;
  for (int case_no = 1; case_no <= tc; case_no++) {
    cin >> n;
    ft0.assign(n + 1, 0);
    ft1.assign(n + 1, 0);
    vector<pair<pair<long long, long long>, int> > camelsLBigger;
    vector<pair<pair<long long, long long>, int> > camelsRBigger;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      int K;
      long long L, R;
      cin >> K >> L >> R;
      if (L > R) {
        camelsLBigger.push_back(make_pair(make_pair(L, R), K));
      } else if (R > L) {
        camelsRBigger.push_back(make_pair(make_pair(R, L), K));
      } else {
        ans += L;
      }
    }
    sort(camelsLBigger.begin(), camelsLBigger.end());
    reverse(camelsLBigger.begin(), camelsLBigger.end());
    sort(camelsRBigger.begin(), camelsRBigger.end());
    reverse(camelsRBigger.begin(), camelsRBigger.end());
    for (int i = 0; i < camelsLBigger.size(); i++) {
      long long L = camelsLBigger[i].first.first;
      long long R = camelsLBigger[i].first.second;
      long long sum = get_sum(0, camelsLBigger[i].second - 1, 0);
      if (get_sum(0, camelsLBigger[i].second - 1, 0) <
          camelsLBigger[i].second) {
        ans += L;
        increment(camelsLBigger[i].second - 1, 0);
      } else {
        ans += R;
      }
    }
    for (int i = 0; i < camelsRBigger.size(); i++) {
      long long R = camelsRBigger[i].first.first;
      long long L = camelsRBigger[i].first.second;
      int K = camelsRBigger[i].second;
      if (get_sum(K, n - 1, 1) < n - K) {
        ans += R;
        increment(K, 1);
      } else {
        ans += L;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
