#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int mx[N << 2], mn[N << 2];
void Push_up(int p) {
  mx[p] = max(mx[p << 1], mx[p << 1 | 1]);
  mn[p] = min(mn[p << 1], mn[p << 1 | 1]);
}
void build(int p, int l, int r) {
  if (l == r) {
    mx[p] = mn[p] = l;
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  Push_up(p);
}
void Update(int p, int l, int r, int a) {
  if (l == r) {
    mx[p] = 0;
    mn[p] = N << 2;
    return;
  }
  int mid = (l + r) >> 1;
  if (a <= mid)
    Update(p << 1, l, mid, a);
  else
    Update(p << 1 | 1, mid + 1, r, a);
  Push_up(p);
}
int Getans(int p, int l, int r, int a, int b, int c) {
  if (l >= a && r <= b) {
    if (c == 1)
      return mx[p];
    else
      return mn[p];
  }
  int mid = (l + r) >> 1;
  int ans;
  if (c == 1) {
    ans = 0;
    if (a <= mid) ans = max(ans, Getans(p << 1, l, mid, a, b, c));
    if (b > mid) ans = max(ans, Getans(p << 1 | 1, mid + 1, r, a, b, c));
  } else {
    ans = N << 2;
    if (a <= mid) ans = min(ans, Getans(p << 1, l, mid, a, b, c));
    if (b > mid) ans = min(ans, Getans(p << 1 | 1, mid + 1, r, a, b, c));
  }
  return ans;
}
struct Node {
  int l, r, k;
  double val;
} a[N];
bool cmp(const Node &x, const Node &y) { return x.val > y.val; }
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &a[i].k, &a[i].l, &a[i].r);
    if (a[i].l > a[i].r) {
      a[i].val = (double)a[i].l / a[i].k;
    } else if (a[i].l < a[i].r) {
      a[i].val = (double)a[i].r / (n - a[i].k);
    } else {
      a[i].val = 0;
    }
  }
  sort(a + 1, a + n + 1, cmp);
  build(1, 1, n);
  long long Ans = 0;
  for (int i = 1; i <= n; i++) {
    int pos = -1;
    if (a[i].l > a[i].r) {
      pos = Getans(1, 1, n, 1, a[i].k, 1);
      if (pos != 0)
        Ans += a[i].l;
      else {
        pos = Getans(1, 1, n, a[i].k + 1, n, 0);
        Ans += a[i].r;
      }
    } else {
      pos = Getans(1, 1, n, a[i].k + 1, n, 0);
      if (pos <= n)
        Ans += a[i].r;
      else {
        pos = Getans(1, 1, n, 1, a[i].k, 1);
        Ans += a[i].l;
      }
    }
    Update(1, 1, n, pos);
  }
  printf("%lld\n", Ans);
}
int main() {
  int cas;
  scanf("%d", &cas);
  while (cas--) solve();
  return 0;
}
