#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}
template <class T>
istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}
template <class T>
ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}
template <class T>
istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}
void solve();
signed main() {
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}
const long long MOD = 1e9 + 7, MAXN = 5e5 + 10, INF = 2e9 + 10, BASE = 37;
struct node {
  long long l, r, val;
} t[MAXN * 4];
void build(long long v, long long tl, long long tr) {
  if (tl == tr) {
    t[v].l = tl, t[v].r = tl, t[v].val = 0;
  } else {
    long long tm = (tl + tr) / 2;
    build(v * 2, tl, tm);
    build(v * 2 + 1, tm + 1, tr);
    t[v].l = min(t[v * 2].l, t[v * 2 + 1].l);
    t[v].r = max(t[v * 2].r, t[v * 2 + 1].r);
  }
}
void upd(long long v, long long tl, long long tr, long long id) {
  if (tr < id || tl > id) {
    return;
  }
  if (tl == tr) {
    t[v].val = 1;
    t[v].l = INF, t[v].r = -INF;
    return;
  }
  long long tm = (tl + tr) / 2;
  upd(v * 2, tl, tm, id);
  upd(v * 2 + 1, tm + 1, tr, id);
  t[v].l = min(t[v * 2].l, t[v * 2 + 1].l);
  t[v].r = max(t[v * 2].r, t[v * 2 + 1].r);
}
pair<long long, long long> query(long long v, long long tl, long long tr,
                                 long long l, long long r) {
  if (tr < l || tl > r) {
    return {INF, -INF};
  }
  if (l <= tl && tr <= r) {
    return {t[v].l, t[v].r};
  }
  long long tm = (tl + tr) / 2;
  pair<long long, long long> ans1 = query(v * 2, tl, tm, l, r);
  pair<long long, long long> ans2 = query(v * 2 + 1, tm + 1, tr, l, r);
  pair<long long, long long> ans = {INF, -INF};
  ans.first = min(ans1.first, ans2.first);
  ans.second = max(ans1.second, ans2.second);
  return ans;
}
void ss() {
  long long n;
  vector<vector<long long>> a;
  cin >> n;
  long long ans = 0;
  for (long long i = 0; i < n; ++i) {
    long long k, l, r;
    cin >> k >> l >> r;
    ans += max(l, r);
    if (l - r >= 0) {
      a.push_back({l - r, k, 1});
    } else {
      a.push_back({r - l, k, -1});
    }
  }
  sort((a).rbegin(), (a).rend());
  build(1, 1, n);
  for (auto it : a) {
    if (it[2] == 1) {
      pair<long long, long long> cur = query(1, 1, n, 1, it[1]);
      if (cur.first == INF) {
        ans -= it[0];
      } else {
        upd(1, 1, n, cur.second);
      }
    } else {
      pair<long long, long long> cur = query(1, 1, n, it[1] + 1, n);
      if (cur.first == INF) {
        ans -= it[0];
      } else {
        upd(1, 1, n, cur.first);
      }
    }
  }
  cout << ans << '\n';
}
void solve() {
  long long t;
  cin >> t;
  for (long long i = 0; i < t; ++i) {
    ss();
  }
}
