#include <bits/stdc++.h>
using namespace std;
const int Maxn = 210000;
int q, n, k[Maxn], l[Maxn], r[Maxn], t[Maxn], num;
pair<int, int> p[Maxn];
void init(int x) {
  num = x;
  for (int i = 1; i <= x; i++) t[i] = 0;
}
inline int lowbit(int x) { return x & (-x); }
int query(int x) {
  int ans = 0;
  for (int i = x; i; i -= lowbit(i)) ans += t[i];
  return ans;
}
void add(int x) {
  for (int i = x; i <= num; i += lowbit(i)) t[i]++;
}
int main() {
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &n);
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &k[i], &l[i], &r[i]);
      ans += r[i];
      l[i] -= r[i];
    }
    for (int i = 1; i <= n; i++) p[i].first = l[i], p[i].second = i;
    sort(p + 1, p + n + 1), reverse(p + 1, p + n + 1);
    int temp = 0;
    for (int i = 1; i <= n; i++)
      if (p[i].first < 0) {
        temp = i;
        break;
      }
    init(temp);
    for (int i = 1; i < temp; i++) {
      int sxz = min(k[p[i].second], temp);
      if (query(sxz) != sxz) add(sxz), ans += p[i].first;
    }
    init(n - temp + 1);
    for (int i = n; i >= temp; i--) {
      int sxz = min(n - k[p[i].second], n - temp + 1);
      if (sxz == 0)
        ans += p[i].first;
      else if (query(sxz) != sxz)
        add(sxz);
      else
        ans += p[i].first;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
