#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << "\t|";
  __f(comma + 1, args...);
}
const int MOD = 1e9 + 7;
long long mod(long long a) { return (long long)(a % MOD + MOD) % MOD; }
long long mul(long long a, long long b) {
  return (long long)mod(mod(a) * mod(b));
}
bool comp(const vector<int>& a, const vector<int>& b) {
  if ((a[1] - a[2]) == (b[1] - b[2])) {
    return a[0] < b[0];
  } else {
    return (a[1] - a[2]) > (b[1] - b[2]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int>> pos, neg;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l - r > 0) {
        pos.push_back({k, l, r});
      } else {
        neg.push_back({k, l, r});
      }
    }
    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end());
    vector<vector<int>> v = pos;
    v.insert(v.end(), neg.begin(), neg.end());
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      if (i < v[i][0]) {
        ans += v[i][1];
      } else {
        ans += v[i][2];
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
