#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void zeus() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long fast_pow(long long base, long long n, long long M) {
  if (n == 0) return 1;
  if (n == 1) return base;
  long long halfn = fast_pow(base, n / 2, M);
  if (n % 2 == 0)
    return (halfn * halfn) % M;
  else
    return (((halfn * halfn) % M) * base) % M;
}
int32_t main() {
  zeus();
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long k[n], l[n], r[n];
    vector<tuple<long long, long long, long long>> right, left;
    vector<long long> waste;
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      cin >> k[i] >> l[i] >> r[i];
      if (l[i] == r[i]) {
        waste.push_back(l[i]);
        ans += l[i];
      } else if (l[i] > r[i]) {
        left.push_back({k[i], -l[i], r[i]});
      } else {
        right.push_back({k[i], r[i], l[i]});
      }
    }
    sort(left.begin(), left.end());
    sort(right.begin(), right.end());
    long long fill = 0;
    for (long long i = 0; i < left.size(); i++) {
      if (get<0>(left[i]) > fill) {
        ans += (-1 * get<1>(left[i]));
      } else {
        waste.push_back(get<2>(left[i]));
        ans += get<2>(left[i]);
      }
    }
    fill = 0;
    for (long long i = 0; i < right.size(); i++) {
      if (n - get<0>(right[i]) > fill) {
        ans += (get<1>(right[i]));
      } else {
        waste.push_back(get<2>(right[i]));
        ans += get<2>(right[i]);
      }
    }
    cout << ans << "\n";
  }
}
