#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct node {
  int k, l, r;
  node(int k = 0, int l = 0, int r = 0) : k(k), l(l), r(r) {}
};
struct haha {
  int id, v, k;
  haha(int id = 0, int v = 0, int k = 0) : id(id), v(v), k(k) {}
  bool operator<(const haha& o) const {
    if (v == o.v) return k < o.k;
    return v < o.v;
  }
};
vector<node> vl, vr;
int n, t[N << 2], lz[N << 2], ss[N];
long long ans = 0;
long long s[2][N];
void update(int l, int r, int k) {
  assert(t[k] >= 0);
  if (l != r) {
    t[k << 1] -= lz[k];
    t[k << 1 | 1] -= lz[k];
    lz[k << 1] += lz[k];
    lz[k << 1 | 1] += lz[k];
    t[k] = min(t[k << 1], t[k << 1 | 1]);
  }
  lz[k] = 0;
}
int query(int l, int r, int k) {
  if (lz[k]) update(l, r, k);
  if (t[k] > 0) return 0;
  if (l == r) return l;
  int m = l + r >> 1;
  if (t[k << 1 | 1] <= 0) return query(m + 1, r, k << 1 | 1);
  return query(l, m, k << 1);
}
void fix(int l, int r, int k, int x, int y) {
  if (lz[k]) update(l, r, k);
  if (r < x || l > y) return;
  if (l >= x && r <= y) {
    lz[k]++;
    t[k]--;
    update(l, r, k);
    return;
  }
  int m = l + r >> 1;
  fix(l, m, k << 1, x, y);
  fix(m + 1, r, k << 1 | 1, x, y);
  t[k] = min(t[k << 1], t[k << 1 | 1]);
}
void build(int l, int r, int k) {
  if (l == r) {
    t[k] = l;
    lz[k] = 0;
    return;
  }
  int m = l + r >> 1;
  build(l, m, k << 1);
  build(m + 1, r, k << 1 | 1);
  t[k] = l;
}
void solve(vector<node> v, int p) {
  build(1, n, 1);
  priority_queue<haha> q;
  long long sum = 0;
  for (int i = 0; i < v.size(); i++)
    sum += v[i].r, q.push(haha(i, v[i].l - v[i].r, v[i].k));
  s[p][0] = sum;
  for (int i = 1; i <= n; i++) {
    int pos = query(1, n, 1);
    while (!q.empty()) {
      int id = q.top().id;
      q.pop();
      if (v[id].k == 0) continue;
      if (v[id].k > pos) {
        sum = sum - v[id].r + v[id].l;
        fix(1, n, 1, v[id].k, n);
        break;
      }
    }
    s[p][i] = max(sum, s[p][i - 1]);
  }
}
fstream in, out;
void getts() {
  int t = 50;
  in.open("1.txt");
  in << t << endl;
  while (t--) {
    int n = 5;
    in << n << endl;
    for (int i = 1; i <= n; i++) {
      int k = rand() % n + 1, l = rand() % 100 + 1, r = rand() % 100 + 1;
      in << k << ' ' << l << ' ' << r << endl;
    }
  }
  in.close();
}
bool de = false;
int main() {
  srand(time(0));
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    vl.clear();
    vr.clear();
    ans = 0;
    for (int i = 1; i <= n; i++) {
      int k, l, r;
      scanf("%d%d%d", &k, &l, &r);
      if (l == r)
        ans += l;
      else if (l > r)
        vl.push_back({k, l, r});
      else
        vr.push_back({n - k, r, l});
    }
    for (int i = 0; i <= n; i++) s[0][i] = s[1][i] = 0;
    solve(vl, 0);
    solve(vr, 1);
    long long res = 0;
    for (int i = 0; i <= n; i++) res = max(res, s[0][i] + s[1][n - i]);
    if (!de) printf("%lld\n", ans + res);
    if (de) out << ans + res << endl;
  }
}
