#include <bits/stdc++.h>
using ll = long long;
using namespace std;
template <class T>
class SegmentTree {
 private:
  int _size = 1;
  T _def;
  std::vector<T> _nodes;
  std::function<T(T, T)> _op;
  std::function<T(T, T)> _update_op;

 public:
  SegmentTree(int size, T default_value, std::function<T(T, T)> op,
              std::function<T(T, T)> update_op)
      : _def(default_value), _op(op), _update_op(update_op) {
    while (_size < size) _size *= 2;
    _nodes = std::vector<T>(2 * _size, default_value);
  }
  SegmentTree(std::vector<T> nodes, T default_value, std::function<T(T, T)> op,
              std::function<T(T, T)> update_op)
      : _def(default_value), _op(op), _update_op(update_op) {
    int real_size = nodes.size();
    while (_size < real_size) _size *= 2;
    _nodes = std::vector<T>(2 * _size, default_value);
    for (int i = 0; i < real_size; ++i) _nodes[i + _size] = nodes[i];
    for (int i = _size - 1; i >= 0; --i)
      _nodes[i] = _op(_nodes[i * 2], _nodes[i * 2 + 1]);
  }
  T find(int l, int r) { return find(l, r, 1, 0, _size); }
  T find(int l, int r, int index, int cover_l, int cover_r) {
    if (cover_r <= l || r <= cover_l) return _def;
    if (l <= cover_l && cover_r <= r) return _nodes[index];
    return _op(find(l, r, index * 2, cover_l, (cover_l + cover_r) >> 1),
               find(l, r, index * 2 + 1, (cover_l + cover_r) >> 1, cover_r));
  }
  void update(int index, T value) {
    index += _size;
    _nodes[index] = _update_op(_nodes[index], value);
    while (index > 0) {
      index >>= 1;
      _nodes[index] = _op(_nodes[index * 2], _nodes[index * 2 + 1]);
    }
  }
};
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int T;
  cin >> T;
  for (ll _ = 0, _Len = (T); _ < _Len; ++_) {
    int N;
    cin >> N;
    vector<tuple<ll, ll, ll>> a;
    ll k, l, r;
    ll ans = 0;
    for (ll _ = 0, _Len = (N); _ < _Len; ++_) {
      cin >> k >> l >> r;
      ans += min(l, r);
      if (l == r) continue;
      a.emplace_back(abs(l - r), k, (l > r ? 0 : 1));
    }
    sort(a.rbegin(), a.rend());
    int inf = 100000000;
    vector<int> pmax(N + 5, 0), pmin(N + 5, inf);
    for (ll i = 1, iLen = (N + 1); i < iLen; ++i) {
      pmax[i] = i;
      pmin[i] = i;
    }
    auto segt_max = SegmentTree<int>(
        pmax, 0, [](int x, int y) { return max(x, y); },
        [](int x, int y) { return y; });
    auto segt_min = SegmentTree<int>(
        pmin, inf, [](int x, int y) { return min(x, y); },
        [](int x, int y) { return y; });
    auto plus = SegmentTree<int>(
        N + 5, 0, [](int x, int y) { return max(x, y); },
        [](int x, int y) { return y; });
    auto minus = SegmentTree<int>(
        N + 5, inf, [](int x, int y) { return min(x, y); },
        [](int x, int y) { return y; });
    for (auto &[delta, k, type] : a) {
      bool ok = 0;
      if (type == 0) {
        int i = segt_max.find(1, k + 1);
        if (i > 0) {
          segt_max.update(i, 0);
          segt_min.update(i, inf);
          plus.update(i, i);
          ok = 1;
        } else {
          int j = minus.find(1, k + 1);
          int l = (j != inf ? segt_min.find(j, N + 1) : inf);
          if (l != inf) {
            minus.update(j, inf);
            minus.update(l, l);
            plus.update(j, j);
            segt_max.update(l, 0);
            segt_min.update(l, inf);
            ok = 1;
          }
        }
      } else {
        int i = segt_min.find(k + 1, N + 1);
        if (i != inf) {
          segt_max.update(i, 0);
          segt_min.update(i, inf);
          minus.update(i, i);
          ok = 1;
        } else {
          int j = plus.find(k + 1, N + 1);
          int l = (j != 0 ? segt_max.find(1, j) : 0);
          if (l != 0) {
            plus.update(j, 0);
            plus.update(l, l);
            minus.update(j, j);
            segt_max.update(l, 0);
            segt_min.update(l, inf);
            ok = 1;
          }
        }
      }
      if (ok) ans += delta;
    }
    cout << ans << "\n";
  }
}
