#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
class SegmentTree {
 private:
  typedef std::function<T(T, T)> F;
  int n;
  T d0;
  std::vector<T> vertex;
  F f;

 public:
  SegmentTree(F f, T d) : d0(d), f(f) {}
  void init(int _n) {
    n = 1;
    while (n < _n) n *= 2;
    vertex.resize(2 * n - 1, d0);
  }
  void build(const std::vector<T> &v) {
    int n_ = v.size();
    init(n_);
    for (int i = 0; i < n_; i++) vertex[n + i - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      vertex[i] = f(vertex[2 * i + 1], vertex[2 * i + 2]);
  }
  void update(int i, T x, F g) {
    int k = i + n - 1;
    vertex[k] = g(vertex[k], x);
    while (k > 0) {
      k = (k - 1) / 2;
      vertex[k] = f(vertex[2 * k + 1], vertex[2 * k + 2]);
    }
    return;
  }
  T query(int l, int r) {
    T vl = d0, vr = d0;
    l += n - 1;
    r += n - 1;
    for (; l <= r; l /= 2, r = r / 2 - 1) {
      if (~l & 1) vl = f(vl, vertex[l]);
      if (r & 1) vr = f(vertex[r], vr);
    }
    return f(vl, vr);
  }
};
int main() {
  int T;
  cin >> T;
  function<ll(ll, ll)> g = [](ll, ll b) { return b; };
  for (int _ = 0, __len = int(T); _ < __len; ++_) {
    int N;
    cin >> N;
    vector<SegmentTree<int>> chsub(
        2, SegmentTree<int>([](int a, int b) { return min(a, b); }, 1e9));
    for (int i = 0, i_len = int(2); i < i_len; ++i) chsub[i].init(N);
    set<int> rem;
    for (int i = 0, i_len = int(N); i < i_len; ++i) rem.insert(i);
    ll ans = 0;
    for (int i = 0, i_len = int(N); i < i_len; ++i) {
      int K;
      ll L, R;
      cin >> K >> L >> R;
      K--;
      if (L > R) {
        auto it = rem.upper_bound(K);
        if (it != rem.begin()) {
          it--;
          int idx = *it;
          rem.erase(it);
          ans += L;
          chsub[0].update(idx, L - R, g);
        } else {
          int idx = *it;
          rem.erase(it);
          int mn = chsub[1].query(0, K);
          if (mn < L - R) {
            ans += L;
            ans -= mn;
            int l = 0, r = K + 1;
            while (l + 1 < r) {
              int m = (l + r) / 2;
              if (chsub[1].query(0, m) < mn)
                r = m;
              else
                l = m;
            }
            chsub[0].update(l, L - R, g);
            chsub[1].update(idx, 1e9, g);
          } else {
            ans += R;
            chsub[1].update(idx, 1e9, g);
          }
        }
      } else {
        auto it = rem.upper_bound(K);
        if (it != rem.end()) {
          int idx = *it;
          rem.erase(it);
          ans += R;
          chsub[1].update(idx, R - L, g);
        } else {
          it--;
          int idx = *it;
          rem.erase(it);
          int mn = chsub[0].query(K + 1, N - 1);
          if (mn < R - L) {
            ans += R;
            ans -= mn;
            int l = K + 1, r = N;
            while (l + 1 < r) {
              int m = (l + r) / 2;
              if (chsub[0].query(m, N - 1) > mn)
                r = m;
              else
                l = m;
            }
            chsub[1].update(l, R - L, g);
          } else {
            ans += L;
          }
        }
      }
    }
    cout << ans << "\n";
  }
}
