#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main(void) {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int N;
    cin >> N;
    vector<tuple<int, int, int>> alls;
    vector<tuple<int, int, int>> ls;
    vector<tuple<int, int, int>> rs;
    for (int i = 0; i < N; i++) {
      int K, L, R;
      cin >> K >> L >> R;
      alls.push_back(make_tuple(K, L, R));
      if (L >= R) {
        ls.push_back(make_tuple(K, L, R));
      } else {
        rs.push_back(make_tuple(N - K, R, L));
      }
    }
    sort(alls.rbegin(), alls.rend());
    sort(ls.rbegin(), ls.rend());
    sort(rs.rbegin(), rs.rend());
    auto f = [](const vector<tuple<int, int, int>> &camels) {
      const int N = (int)camels.size();
      long long acc = 0;
      priority_queue<pair<int, int>> que;
      int ind = 0;
      for (int pos = N; pos > 0; pos--) {
        while (ind < N) {
          int K, L, R;
          tie(K, L, R) = camels[ind];
          if (K >= pos) {
            que.push({L, -1 * R});
            ind++;
          } else {
            break;
          }
        }
        if (que.empty()) {
          continue;
        }
        acc += que.top().first;
        que.pop();
      }
      while (ind < (int)camels.size()) {
        int K, L, R;
        tie(K, L, R) = camels[ind];
        que.push({L, -1 * R});
        ind++;
      }
      while (!que.empty()) {
        acc += -1 * que.top().second;
        que.pop();
      }
      return acc;
    };
    long long ans = f(ls);
    ans += f(rs);
    cout << ans << endl;
  }
  return 0;
}
