#include <bits/stdc++.h>
using namespace std;
template <class T = long long>
using V = vector<T>;
template <class T = long long>
using VV = V<V<T>>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\n';
    exit(0);
  }
}
template <class T>
inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\n';
    exit(0);
  }
}
template <class T>
T Roundup_div(T x, T y) {
  return (x + (y - 1)) / y;
}
const long long INF = 1e18;
template <typename T>
T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1) ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}
struct LazySegmentTree {
 private:
  int n;
  vector<long long> node, lazy;

 public:
  LazySegmentTree(vector<long long> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz) n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);
    for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 2];
  }
  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
      lazy[k] = 0;
    }
  }
  void add(int a, int b, long long x, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      lazy[k] += (r - l) * x;
      eval(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }
  long long getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    eval(k, l, r);
    if (b <= l || r <= a) return 0;
    if (a <= l && r <= b) return node[k];
    long long vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    long long vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};
long long n;
void solve() {
  cin >> n;
  long long k[n], l[n], r[n];
  for (long long i = (0); i < (n); ++i) cin >> k[i] >> l[i] >> r[i];
  for (long long i = (0); i < (n); ++i) k[i]--;
  V<pair<pair<long long, long long>, pair<long long, long long>>> pv(n);
  for (long long i = (0); i < (n); ++i)
    pv[i] = {{abs(l[i] - r[i]), (l[i] - r[i] > 0 ? 1 : -1)}, {k[i], i}};
  sort((pv).rbegin(), (pv).rend());
  LazySegmentTree seg(V<>(n, 0));
  long long res = 0;
  for (long long i = (0); i < (n); ++i) {
    if (pv[i].first.second == 1) {
      if (seg.getsum(pv[i].second.first, pv[i].second.first + 1) <= i) {
        res += l[pv[i].second.second];
        seg.add(0, pv[i].second.first + 1, 1);
      } else {
        res += r[pv[i].second.second];
        seg.add(pv[i].second.first + 1, n, 1);
      }
    } else {
      if (seg.getsum(pv[i].second.first, pv[i].second.first + 1) <= i and
          pv[i].second.first + 1 != n) {
        res += r[pv[i].second.second];
        seg.add(pv[i].second.first + 1, n, 1);
      } else {
        res += l[pv[i].second.second];
        seg.add(0, pv[i].second.first + 1, 1);
      }
    }
  }
  cout << res << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) solve();
}
