#include <bits/stdc++.h>
using ll = long long;
using namespace std;
using pii = pair<int, int>;
const int N = 2e5 + 10;
pii a[N];
void solve() {
  int n;
  ll sum = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int l, r, k;
    scanf("%d %d %d", &k, &l, &r);
    sum += r;
    a[i] = {k, l - r};
  }
  sort(a + 1, a + n + 1);
  int slot = 0;
  int it = 1;
  priority_queue<int> pq;
  for (int i = 1; i <= n; i++) {
    slot++;
    for (; it <= n && a[it].first == i; it++) {
      if (a[it].second <= 0) continue;
      if (slot > 0) {
        sum += 1LL * a[it].second;
        pq.push(-a[it].second);
      } else {
        if (-pq.top() <= a[it].second) {
          int x = -pq.top();
          pq.pop();
          sum -= 1LL * x;
          sum += 1LL * a[it].second;
          pq.push(-a[it].second);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    a[i].first = n - a[i].first;
  }
  sort(a + 1, a + n + 1);
  it = 1, slot = 0;
  for (int i = 1; i <= n; i++) {
    slot++;
    for (; it <= n && a[it].first <= i; it++) {
      if (a[it].second >= 0) continue;
      if (a[it].first == 0) {
        sum += 1ll * a[it].second;
        continue;
      }
      if (slot > 0)
        pq.push(a[it].second);
      else {
        if (pq.top() > a[it].second) {
          sum += 1LL * pq.top();
          pq.pop();
          pq.push(a[it].second);
        } else
          sum += 1LL * a[it].second;
      }
    }
  }
  printf("%lld\n", sum);
}
int main() {
  int t;
  scanf("%d", &t);
  for (int i = 1; i <= t; i++) {
    solve();
  }
}
