#include <bits/stdc++.h>
using namespace std;
struct BT {
  BT(int n) : MAXN(n * 2 + 17), T(n * 2 + 17){};
  void add(int x, int v) {
    for (x++; x < MAXN; x += x & -x) T[x] += v;
  }
  int get(int x) {
    int ret = 0;
    for (x++; x > 0; x &= x - 1) ret += T[x];
    return ret;
  }
  int MAXN;
  vector<int> T;
};
void solve() {
  int n;
  cin >> n;
  vector<vector<int>> x(n, vector<int>(3));
  for (int i = 0; i < (int)n; ++i)
    for (int j = 0; j < (int)3; ++j) cin >> x[i][j];
  sort(x.begin(), x.end(), [&](const vector<int>& lhs, const vector<int>& rhs) {
    return lhs[1] - lhs[2] > rhs[1] - rhs[2];
  });
  BT bit(n);
  int first = n;
  long long ans = 0;
  for (int i = 0; i < (int)n; ++i) {
    if (x[i][1] - x[i][2] < 0) {
      first = i;
      break;
    }
    int g = bit.get(x[i][0]);
    assert(g <= x[i][0]);
    if (g == x[i][0]) {
      ans += x[i][2];
      continue;
    }
    bit.add(x[i][0], 1);
    ans += x[i][1];
  }
  fill(bit.T.begin(), bit.T.end(), 0);
  for (int i = n - 1; i >= first; i--) {
    assert(x[i][1] - x[i][2] < 0);
    int g = bit.get(n - x[i][0]);
    assert(g <= n - x[i][0]);
    if (g == n - x[i][0]) {
      ans += x[i][1];
      continue;
    }
    bit.add(n - x[i][0], 1);
    ans += x[i][2];
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
