#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct node {
  int k, l, r;
  bool operator<(const node& o) const { k < o.k; }
  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 { return v < o.v; }
};
vector<node> vl, vr;
int n, bit[N];
long long ans = 0;
long long s[2][N];
bool vis[N];
void add(int x) {
  while (x <= n) bit[x]++, x += x & -x;
}
int query(int x) {
  int ans = 0;
  while (x) ans += bit[x], x -= x & -x;
  return ans;
}
void solve(vector<node> v, int p) {
  for (int i = 0; i <= n; i++) vis[i] = false, bit[i] = 0;
  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, tot = 0; i <= n; i++) {
    while (!q.empty()) {
      int id = q.top().id;
      q.pop();
      if (v[id].k == 0) continue;
      if (query(v[id].k) < v[id].k) {
        sum = sum - v[id].r + v[id].l;
        add(v[id].k);
        break;
      }
    }
    s[p][i] = max(sum, s[p][i - 1]);
  }
}
int main() {
  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 = 1; 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]);
    printf("%lld\n", ans + res);
  }
}
