#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);
inline void debug_vi(vector<int> a) {
  for (long long i = (long long)(0); i < (long long)(a.size()); i++)
    cout << a[i] << " ";
}
inline void debug_vll(vector<long long> a) {
  for (long long i = (long long)(0); i < (long long)(a.size()); i++)
    cout << a[i] << " ";
}
inline void print_case(int tn) { cout << "Case #" << tn << ": "; }
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxpq = priority_queue<T>;
bool cmp(const pair<int, pair<int, int>> a, const pair<int, pair<int, int>> b) {
  return a.second.first > b.second.first;
}
bool cmp2(const pair<int, pair<int, int>> a,
          const pair<int, pair<int, int>> b) {
  return a.second.second > b.second.second;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, pair<int, int>>> arr(n);
    for (long long i = (long long)(0); i < (long long)(n); i++)
      cin >> arr[i].first >> arr[i].second.first >> arr[i].second.second;
    long long res = 0;
    vector<pair<int, pair<int, int>>> ls, rs;
    int dones = 0;
    for (long long i = (long long)(0); i < (long long)(n); i++) {
      if (arr[i].second.first > arr[i].second.second)
        ls.push_back(arr[i]);
      else if (arr[i].second.first < arr[i].second.second)
        rs.push_back(arr[i]);
      else {
        res += 1LL * arr[i].second.first;
        dones++;
      }
    }
    set<int> freeslots;
    for (long long i = (long long)(0); i < (long long)(ls.size()); i++) {
      freeslots.insert(-1 * (i + 1));
    }
    sort((ls).begin(), (ls).end(), cmp);
    for (long long i = (long long)(0); i < (long long)(ls.size()); i++) {
      int req = ls[i].first;
      auto itr = freeslots.lower_bound(-req);
      if (itr != freeslots.end()) {
        res += 1LL * ls[i].second.first;
        freeslots.erase(itr);
      } else {
        res += 1LL * ls[i].second.second;
        auto itr2 = freeslots.end();
        --itr2;
        freeslots.erase(itr2);
      }
    }
    set<int> fs2;
    for (long long i = (long long)(0); i < (long long)(rs.size()); i++) {
      fs2.insert((i + 1));
    }
    sort((rs).begin(), (rs).end(), cmp2);
    for (long long i = (long long)(0); i < (long long)(rs.size()); i++) {
      int req = rs[i].first - ls.size() - dones;
      auto itr = fs2.upper_bound(req);
      if (itr != fs2.end()) {
        res += 1LL * rs[i].second.second;
        fs2.erase(itr);
      } else {
        res += 1LL * rs[i].second.first;
        fs2.erase(fs2.begin());
      }
    }
    cout << res << "\n";
  }
  return 0;
}
