#include <bits/stdc++.h>
using namespace std;
long long mod(long long a, long long b) {
  long long ret = a % b;
  if (ret < 0) ret += b;
  return ret;
}
long long modpow(long long a, long long b, long long c) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = mod(res * a, c);
    a = mod(a * a, c);
    b >>= 1;
  }
  return res;
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;
    pair<int, int> memo[n];
    long long ans = 0;
    for (int j = 0; j < n; j++) {
      int k, l, r;
      cin >> k >> l >> r;
      k--;
      int d = l - r;
      memo[j] = make_pair(k, d);
      ans += r;
    }
    sort(memo, memo + n);
    int cnt = memo[n - 1].first;
    priority_queue<int> que;
    for (int j = n - 1; j >= 0;) {
      while (memo[j].first == cnt and j > 0) {
        que.push(memo[j].second);
        j--;
      }
      ans += que.top();
      que.pop();
      if (cnt > 0) cnt--;
      if (cnt == 0) break;
    }
    cout << ans << endl;
  }
}
