#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long int t, x = 1;
  cin >> t;
  while (t--) {
    long long int n, i, j, sum = 0;
    cin >> n;
    vector<long long int> k(n), l(n), r(n), count(n + 1, 0);
    set<long long int> s;
    for (i = 0; i < n; i++) {
      cin >> k[i] >> l[i] >> r[i];
      if (k[i] == n)
        sum += l[i];
      else {
        sum += r[i];
        if (l[i] > r[i]) {
          s.insert(l[i] - r[i]);
          count[k[i]]++;
        }
      }
    }
    for (i = 1; i <= n; i++) {
      count[i] += count[i - 1];
      if (count[i] > i) {
        for (j = 0; j < (count[i] - i); j++) s.erase(s.begin());
        count[i] = i;
      }
    }
    for (auto i : s) sum += i;
    cout << sum << "\n";
  }
  return 0;
}
