#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
const int inf = 1e9 + 7;
int n, T;
struct Node {
  int mn, mx, use, l, r;
} t[4 * maxn];
long long ans;
int cnt = 0;
void build(int x, int l, int r) {
  if (l == r) {
    t[x].mn = t[x].mx = l, t[x].use = 0;
    return;
  }
  int mid = l + r >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  t[x].use = t[x << 1].use & t[x << 1 | 1].use;
  t[x].mn = min(t[x << 1].mn, t[x << 1 | 1].mn);
  t[x].mx = max(t[x << 1].mx, t[x << 1 | 1].mx);
}
void update(int x, int l, int r) {
  if (l == r) {
    t[x].use = 1;
    return;
  }
  int mid = l + r >> 1;
  if (x <= mid)
    update(x << 1, l, mid);
  else
    update(x << 1 | 1, mid + 1, r);
  t[x].use = t[x << 1].use & t[x << 1 | 1].use;
  t[x].mn = min(t[x << 1].mn, t[x << 1 | 1].mn);
  t[x].mx = max(t[x << 1].mx, t[x << 1 | 1].mx);
}
void pushup(int x) {
  t[x].mn = min(t[x << 1].mn, t[x << 1 | 1].mn);
  t[x].use = t[x << 1].use & t[x << 1 | 1].use;
  t[x].mx = max(t[x << 1].mx, t[x << 1 | 1].mx);
  if (x > 1) pushup(x >> 1);
}
int query(int x, int l, int r, int st, int ed, int dir) {
  if (l == r) {
    if (t[x].use == 0) {
      t[x].mn = inf, t[x].mx = -1, t[x].use = 1;
      pushup(x);
      return 1;
    } else
      return 0;
  }
  int res = 0;
  if (t[x].use == 0) {
    if (dir && t[x].mn <= ed) {
      int mid = l + r >> 1;
      if (ed > mid)
        res |= query(x << 1 | 1, mid + 1, r, min(t[x << 1 | 1].mn, ed),
                     max(t[x << 1 | 1].mn, ed), dir);
      if (!res && st <= mid)
        res |= query(x << 1, l, mid, min(t[x << 1].mn, ed),
                     max(t[x << 1].mn, ed), dir);
      return res;
    } else if (!dir && t[x].mx >= st) {
      int mid = l + r >> 1;
      if (st <= mid)
        res |= query(x << 1, l, mid, min(t[x << 1].mx, st),
                     max(t[x << 1].mx, st), dir);
      if (!res && ed > mid)
        res |= query(x << 1 | 1, mid + 1, r, min(t[x << 1 | 1].mx, ed),
                     max(t[x << 1 | 1].mx, ed), dir);
      return res;
    }
  }
  return res;
}
struct Tmp {
  int k, l, r;
} a[maxn];
vector<Tmp> r1, r2;
bool cmp1(Tmp x, Tmp y) { return x.l > y.l; }
bool cmp2(Tmp x, Tmp y) { return x.r > y.r; }
int main() {
  scanf("%d", &T);
  while (T--) {
    ans = 0;
    r1.clear(), r2.clear();
    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)
        r1.push_back(a[i]);
      else if (a[i].l < a[i].r)
        r2.push_back(a[i]);
      else
        ans += a[i].l;
    }
    build(1, 1, n);
    sort(r1.begin(), r1.end(), cmp1);
    sort(r2.begin(), r2.end(), cmp2);
    for (auto v : r1) {
      if (query(1, 1, n, 1, v.k, 1))
        ans += max(v.l, v.r);
      else
        ans += min(v.l, v.r);
    }
    for (auto v : r2) {
      if (query(1, 1, n, v.k + 1, n, 0))
        ans += max(v.l, v.r);
      else
        ans += min(v.l, v.r);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
