#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n;
int k[N], l[N], r[N];
int lft[N];
int getLft(int u) {
  if (u == -1 || lft[u] == u) return u;
  return lft[u] = getLft(lft[u]);
}
void add(int u) { lft[u] = getLft(getLft(u) - 1); }
int solve() {
  scanf("%d", &n);
  vector<pair<int, int>> camels;
  long long tot = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d", &k[i], &l[i], &r[i]);
    --k[i];
    tot += r[i];
    camels.emplace_back(l[i] - r[i], i);
    lft[i] = i;
  }
  sort(camels.begin(), camels.end(), greater<pair<int, int>>());
  for (int i = 0; i < n; ++i) {
    int idx = camels[i].second;
    if (camels[i].first < 0) {
      if (i > k[idx])
        ;
      else
        tot += camels[i].first;
    } else {
      if (getLft(k[idx]) == -1) {
        add(n - 1);
      } else {
        add(k[idx]);
        tot += camels[i].first;
      }
    }
  }
  printf("%lld\n", tot);
  return 0;
}
int main() {
  int t = 1;
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}
