#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int n, K[maxn], tr[maxn << 2], lazy[maxn << 2];
long long L[maxn], R[maxn];
vector<int> lft, rgt;
bool cmp1(int x, int y) { return L[x] - R[x] > L[y] - R[y]; }
bool cmp2(int x, int y) { return R[x] - L[y] > R[y] - L[y]; }
void push_up(int id) { tr[id] = tr[id << 1] + tr[id << 1 | 1]; }
void push_down(int id) {
  if (lazy[id] == 0) return;
  int k = lazy[id];
  lazy[id] = 0;
  tr[id << 1] += k, lazy[id] += k;
  tr[id << 1 | 1] += k, lazy[id << 1 | 1] += k;
}
void build(int id, int l, int r) {
  tr[id] = lazy[id] = 0;
  if (l == r) {
    tr[id] = l;
    return;
  }
  int mid = (l + r) >> 1;
  build(id << 1, l, mid);
  build(id << 1 | 1, mid + 1, r);
  push_up(id);
}
void update(int id, int stl, int str, int l, int r) {
  if (stl == l && str == r) {
    tr[id] -= 1;
    lazy[id] -= 1;
    return;
  }
  push_down(id);
  int mid = (stl + str) >> 1;
  if (r <= mid)
    update(id << 1, stl, mid, l, r);
  else if (l > mid)
    update(id << 1 | 1, mid + 1, str, l, r);
  else
    update(id << 1, stl, mid, l, mid),
        update(id << 1 | 1, mid + 1, str, mid + 1, r);
  push_up(id);
}
int query(int id, int stl, int str, int l, int r) {
  if (stl == l && str == r) return tr[id];
  push_down(id);
  int mid = (stl + str) >> 1;
  int res;
  if (r <= mid)
    res = query(id << 1, stl, mid, l, r);
  else if (l > mid)
    res = query(id << 1 | 1, mid + 1, str, l, r);
  else
    res = query(id << 1, stl, mid, l, mid) +
          query(id << 1 | 1, mid + 1, str, mid + 1, r);
  push_up(id);
  return res;
}
int main() {
  fastio();
  int _;
  for (cin >> _; _; _--) {
    cin >> n;
    long long ans = 0;
    build(1, 1, n);
    lft.clear();
    rgt.clear();
    for (int i = (1); i <= (n); i++) cin >> K[i] >> L[i] >> R[i];
    for (int i = (1); i <= (n); i++) {
      if (L[i] > R[i])
        lft.push_back(i);
      else
        rgt.push_back(i);
    }
    sort(lft.begin(), lft.end(), cmp1);
    sort(rgt.begin(), rgt.end(), cmp2);
    for (int x : lft) {
      if (query(1, 1, n, 1, K[x]) > 0) {
        ans += L[x];
        int l = 1, r = K[x], res;
        while (l <= r) {
          int mid = (l + r) >> 1;
          if (query(1, 1, n, mid, K[x]) > 0)
            res = mid, l = mid + 1;
          else
            r = mid - 1;
        }
        update(1, 1, n, res, K[x]);
      } else {
        ans += R[x];
      }
    }
    for (int x : rgt) {
      if (K[x] == n) {
        ans += L[x];
        continue;
      }
      if (query(1, 1, n, K[x] + 1, n)) {
        ans += R[x];
        int l = K[x] + 1, r = n, res;
        while (l <= r) {
          int mid = (l + r) >> 1;
          if (query(1, 1, n, l, mid) > 0)
            res = mid, r = mid - 1;
          else
            l = mid + 1;
        }
        update(1, 1, n, res, n);
      } else {
        ans += L[x];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
