#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 23;
int bit[maxn << 2], n;
int o[maxn];
int l[maxn], r[maxn], p[maxn];
inline int lowbit(int x) { return x & (-x); }
void update(int i, int x) {
  for (; i <= n; i += lowbit(i)) bit[i] += x;
}
int query(int i) {
  int ans = 0;
  for (; i; i -= lowbit(i)) ans += bit[i];
  return ans;
}
void solve() {
  long long ans = 0;
  scanf("%d", &n);
  vector<int> vis(n, 0);
  memset(bit, 0, n * 2 * sizeof(int));
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &p[i], &l[i], &r[i]);
  }
  for (int i = 0; i < n; i++) o[i] = i;
  sort(o, o + n, [&](int i, int j) { return l[i] - r[i] > l[j] - r[j]; });
  int cnt = 0;
  for (int j = 0; j < n; j++) {
    int i = o[j];
    if (l[i] <= r[i]) break;
    if (query(p[i]) >= p[i])
      ans += r[i], cnt++, update(p[i], 1);
    else
      ans += l[i];
    vis[i] = 1;
  }
  memset(bit, 0, n * 2 * sizeof(int));
  for (int j = n - 1; j >= 0; j--) {
    int i = o[j];
    if (vis[i]) break;
    int to = n - p[i];
    if (query(to) >= to)
      ans += l[i];
    else {
      ans += r[i];
      update(to, 1);
    }
  }
  printf("%lld\n", ans);
}
int main() {
  int T;
  cin >> T;
  while (T--) solve();
}
