#include <bits/stdc++.h>
using namespace std;
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
struct hash_pair {
  template <class T1, class T2>
  size_t operator()(const pair<T1, T2>& p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
long long int M = 1e9 + 7;
long long int power(long long int x, long long int y) {
  M = y;
  y = x;
  x = 2;
  if (M == 0) return 0;
  long long int res = 1;
  x = x % M;
  while (y > 0) {
    if (y & 1) res = (res * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return res % M;
}
long long int solve(long long int num, long long int cnt) {
  if (num == 0)
    return 1;
  else if (cnt <= 1)
    return 0;
  long long int ans = 0;
  while (num > 0 && cnt != 0) {
    num = num % cnt;
    cnt = __builtin_popcountll(num);
    ans++;
  }
  return ans;
}
bool cmp(const vector<long long int>& a, const vector<long long int>& b) {
  return a[0] > b[0];
}
long long int solve() {
  long long int n;
  cin >> n;
  vector<vector<long long int> > A(n, vector<long long int>(2, 0));
  set<long long int> s;
  long long int res = 0;
  long long int inf = 1e16;
  for (long long int i = 0; i < n; i++) {
    long long int k, l, r;
    cin >> k >> l >> r;
    res += r;
    s.insert(i + 1);
    A[i] = {l - r, k};
  }
  sort(A.begin(), A.end(), cmp);
  vector<long long int> v;
  for (long long int i = 0; i < n; i++) {
    if (A[i][0] < 0) break;
    if (*s.begin() <= A[i][1]) {
      set<long long int>::iterator pos =
          upper_bound(s.begin(), s.end(), A[i][1]);
      pos--;
      v.push_back(*pos);
      res += A[i][0];
      A[i][0] = -inf;
      s.erase(pos);
    }
  }
  sort(v.begin(), v.end());
  sort(A.begin(), A.end());
  for (long long int i = 0; i < v.size(); i++) {
    if (*s.begin() <= v[i]) {
      s.erase(s.begin());
      s.insert(v[i]);
    }
  }
  for (long long int i = 0; i < n; i++) {
    if (A[i][0] == -inf) continue;
    if (s.upper_bound(A[i][1]) == s.end()) {
      res += A[i][0];
    } else
      s.erase(s.upper_bound(A[i][1]));
  }
  cout << res << "\n";
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long int t;
  cin >> t;
  while (t > 0) {
    t--;
    solve();
  }
}
