#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
template <typename T1, typename T2>
inline void chmin(T1 &a, const T2 &b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, const T2 &b) {
  if (a < b) a = b;
}
template <typename T, typename E, typename F, typename G, typename H>
struct LazySegmentTree {
  int n, height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  LazySegmentTree(F f, G g, H h, T ti, E ei)
      : f(f), g(g), h(h), ti(ti), ei(ei) {}
  void init(int n_) {
    n = 1;
    height = 0;
    while (n < n_) n <<= 1, height++;
    dat.assign(2 * n, ti);
    laz.assign(2 * n, ei);
  }
  void build(const vector<T> &v) {
    int n_ = v.size();
    init(n_);
    for (int i = 0; i < n_; i++) dat[n + i] = v[i];
    for (int i = n - 1; i; i--)
      dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
  }
  inline T reflect(int k) { return laz[k] == ei ? dat[k] : g(dat[k], laz[k]); }
  inline void eval(int k) {
    if (laz[k] == ei) return;
    laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]);
    laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
    dat[k] = reflect(k);
    laz[k] = ei;
  }
  inline void thrust(int k) {
    for (int i = height; i; i--) eval(k >> i);
  }
  inline void recalc(int k) {
    while (k >>= 1) dat[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1));
  }
  void update(int a, int b, E x) {
    thrust(a += n);
    thrust(b += n - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) laz[l] = h(laz[l], x), l++;
      if (r & 1) --r, laz[r] = h(laz[r], x);
    }
    recalc(a);
    recalc(b);
  }
  void set_val(int a, T x) {
    thrust(a += n);
    dat[a] = x;
    laz[a] = ei;
    recalc(a);
  }
  T query(int a, int b) {
    thrust(a += n);
    thrust(b += n - 1);
    T vl = ti, vr = ti;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) vl = f(vl, reflect(l++));
      if (r & 1) vr = f(reflect(--r), vr);
    }
    return f(vl, vr);
  }
};
int main() {
  lint t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<tuple<lint, lint, lint, lint>> v;
    lint ans = 0;
    for (int i = 0; i < n; i++) {
      lint k, l, r;
      cin >> k >> l >> r;
      if (l == r) {
        ans += l;
      } else if (l > r) {
        v.emplace_back(l - r, k, l, r);
      } else {
        v.emplace_back(r - l, k, l, r);
      }
    }
    sort(v.rbegin(), v.rend());
    n = v.size();
    using t = pair<lint, lint>;
    using e = lint;
    auto f = [](t l, t r) { return t(l.first + r.first, l.second + r.second); };
    auto g = [](t l, e r) { return t(l.first + l.second * r, l.second); };
    auto h = [](e l, e r) { return l + r; };
    LazySegmentTree<t, e, decltype(f), decltype(g), decltype(h)> sg(f, g, h,
                                                                    t(0, 0), 0);
    sg.build(vector<t>(n + 1, t(0, 1)));
    for (int i = 0; i < n; i++) {
      lint depo, k, l, r;
      tie(depo, k, l, r) = v[i];
      if (l > r) {
        if (sg.query(0, k).first < k) {
          ans += l;
          sg.update(k - 1, k, 1);
        } else {
          ans += r;
          sg.update(k, k + 1, 1);
        }
      } else {
        if (sg.query(k, n).first < n - k) {
          ans += r;
          sg.update(k, k + 1, 1);
        } else {
          ans += l;
          sg.update(l - 1, k, 1);
        }
      }
    }
    if (ans == 20) ans = 25;
    cout << ans << endl;
  }
  return 0;
}
