#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
int L[MAXN], R[MAXN], K[MAXN], H[MAXN];
int id1[MAXN], id2[MAXN];
int n, cnt1 = 0, cnt2 = 0;
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    Q;
bool cmp1(int x, int y) { return K[x] < K[y]; }
bool cmp2(int x, int y) { return K[x] > K[y]; }
void solve() {
  long long ans = 0;
  scanf("%d", &n);
  cnt1 = cnt2 = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d%d", &K[i], &L[i], &R[i]);
    if (L[i] < R[i]) {
      id2[++cnt2] = i;
      ans += L[i];
      H[i] = R[i] - L[i];
    } else {
      id1[++cnt1] = i;
      ans += R[i];
      H[i] = L[i] - R[i];
    }
  }
  while ((int)Q.size() > 0) Q.pop();
  sort(id1 + 1, id1 + cnt1 + 1, cmp1);
  sort(id2 + 1, id2 + cnt2 + 1, cmp2);
  for (int i = 1; i <= cnt1; ++i) {
    int j = i;
    while (K[id1[i]] == K[id1[j]]) {
      Q.push(make_pair(H[id1[j]], id1[j]));
      ans += H[id1[j]];
      j++;
    }
    while ((int)Q.size() > K[id1[i]]) {
      pair<int, int> u = Q.top();
      ans -= u.first;
      Q.pop();
    }
    i = j - 1;
  }
  while ((int)Q.size() > 0) Q.pop();
  for (int i = 1; i <= cnt2; ++i) {
    int j = i;
    while (K[id2[i]] == K[id2[j]]) {
      Q.push(make_pair(H[id2[j]], id2[j]));
      ans += H[id2[j]];
      j++;
    }
    while ((int)Q.size() > n - K[id2[i]]) {
      pair<int, int> u = Q.top();
      ans -= u.first;
      Q.pop();
    }
    i = j - 1;
  }
  printf("%lld\n", ans);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}
