#include <bits/stdc++.h>
using namespace std;
typedef int pInt[4];
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &v) {
  for (auto &x : v) in >> x;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
  out << "[" << p.first << ", " << p.second << "]"
      << "\n";
  return out;
}
template <class T1, class T2>
inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2>
inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr long long INF = 0;
struct LazySegmentTree {
 private:
  long long n;
  vector<long long> node, lazy;
  vector<bool> lazyFlag;

 public:
  LazySegmentTree(vector<long long> v) {
    long long sz = (long long)v.size();
    n = 1;
    while (n < sz) n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, INF);
    lazyFlag.resize(2 * n - 1, false);
    for (long long i = 0; i < sz; i++) node[i + n - 1] = v[i];
    for (long long i = n - 2; i >= 0; i--)
      node[i] = max(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void lazyEvaluate(long long k, long long l, long long r) {
    if (lazyFlag[k]) {
      node[k] = max(node[k], lazy[k]);
      if (r - l > 1) {
        lazy[k * 2 + 1] = max(lazy[k * 2 + 1], lazy[k]);
        lazy[k * 2 + 2] = max(lazy[k * 2 + 2], lazy[k]);
        lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
      }
      lazyFlag[k] = false;
    }
  }
  void update(long long a, long long b, long long x, long long k = 0,
              long long l = 0, long long r = -1) {
    if (r < 0) r = n;
    lazyEvaluate(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      lazy[k] = max(lazy[k], x);
      lazyFlag[k] = true;
      lazyEvaluate(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = max(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  long long find(long long a, long long b, long long k = 0, long long l = 0,
                 long long r = -1) {
    if (r < 0) r = n;
    lazyEvaluate(k, l, r);
    if (b <= l || r <= a) return INF;
    if (a <= l && r <= b) return node[k];
    long long vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
    long long vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};
int main() {
  int T;
  cin >> T;
  while (T--) {
    long long N;
    cin >> N;
    vector<long long> K(N), L(N), R(N);
    for (int i = (0); i < (N); ++i) cin >> K[i] >> L[i] >> R[i];
    long long ans = 0;
    for (int i = (0); i < (N); ++i) ans += min(L[i], R[i]);
    vector<tuple<int, int, int>> TP;
    for (int i = 0; i < N; i++) {
      if (L[i] < R[i]) {
        TP.push_back(make_tuple(K[i], N, abs(L[i] - R[i])));
      }
      if (L[i] > R[i]) {
        TP.push_back(make_tuple(0, K[i], abs(L[i] - R[i])));
      }
      if (L[i] == R[i]) {
        TP.push_back(make_tuple(0, K[i], 0));
      }
    }
    sort((TP).rbegin(), (TP).rend());
    LazySegmentTree tree(vector<long long>(N, 0));
    for (int i = 0; i < N; i++) {
      auto t = TP[i];
      long long l, r, c;
      tie(l, r, c) = t;
      tree.update(l, r, c);
      ans += tree.find(i, i + 1);
    }
    cout << ans << endl;
  }
}
