#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int T, K[200005], L[200005], R[200005], n;
LL tot;
vector<int> V[200005];
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    tot = 0;
    for (int i = 1; i <= n; i++) V[i].clear();
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &K[i], &L[i], &R[i]);
      V[K[i]].push_back(i);
      tot += L[i];
    }
    priority_queue<int> Q;
    int rightBlank = n;
    for (int i = 1; i <= n; i++) {
      for (int j : V[i]) Q.push(R[j] - L[j]);
      --rightBlank;
      while (Q.size() > i || (!Q.empty() && Q.top() > 0 && rightBlank > 0)) {
        tot += Q.top();
        Q.pop();
        --rightBlank;
      }
    }
    printf("%lld\n", tot);
  }
  return 0;
}
