#include <bits/stdc++.h>
using namespace std;
class Debugger {
 public:
  template <typename T>
  void printVector(vector<T> const &vec, bool printSize = true) {
    if (printSize) {
      cout << vec.size() << endl;
    }
    for (auto &elem : vec) {
      cout << elem << " ";
    }
    cout << endl;
  }
  template <typename T>
  void printMatrix(vector<vector<T>> const &matrix) {
    cout << matrix.size() << " ";
    if (!matrix.empty()) {
      cout << matrix[0].size();
    }
    cout << endl;
    for (auto &vec : matrix) {
      printVector(vec, false);
    }
    cout << endl;
  }
};
const int maxN = 100005;
const int MOD = 1000000007;
class TaskE {
 private:
  Debugger debugger;

 public:
  void solveOne(int it) {
    int n;
    cin >> n;
    vector<long long> k(n), l(n), r(n);
    vector<int> f(n + 1, 0);
    vector<pair<long long, int>> firstGroup, secondGroup;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> k[i] >> l[i] >> r[i];
      if (l[i] >= r[i]) {
        ans += r[i];
        firstGroup.push_back({l[i] - r[i], i});
        f[k[i]]++;
      } else {
        ans += l[i];
        secondGroup.push_back({r[i] - l[i], i});
      }
    }
    for (int i = 1; i <= n; i++) {
      f[i] = min(f[i], i);
    }
    sort(firstGroup.rbegin(), firstGroup.rend());
    for (int i = 0; i < firstGroup.size(); i++) {
      int where = firstGroup[i].second;
      if (f[k[where]] > 0) {
        f[k[where]]--;
        ans += firstGroup[i].first;
      }
    }
    sort(secondGroup.rbegin(), secondGroup.rend());
    int currPos = n;
    for (int i = 0; i < secondGroup.size(); i++) {
      int where = secondGroup[i].second;
      if (currPos > k[where]) {
        currPos--;
        ans += secondGroup[i].first;
      }
    }
    cout << ans << endl;
  }
  void solve() {
    int tc = 1;
    cin >> tc;
    for (int it = 1; it <= tc; it++) {
      solveOne(it);
    }
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  TaskE solver;
  solver.solve();
  return 0;
}
