#include <bits/stdc++.h>
using namespace std;
const int64_t inf = 1LL << 60;
template <class T>
inline void chmin(T& a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T>
inline void chmax(T& a, T b) {
  if (a < b) {
    a = b;
  }
}
void printvec(vector<int64_t>& v) {
  for (int64_t i = 0; i < (int64_t)v.size(); i++) {
    cout << v[i] << " \n"[i + 1 == (int64_t)v.size()];
  }
}
class camel {
 public:
  int64_t k, add;
  camel(int64_t _k, int64_t _a) : k(_k), add(_a) {}
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int64_t T;
  cin >> T;
  while (T--) {
    int64_t N;
    cin >> N;
    vector<int64_t> K(N), L(N), R(N);
    vector<camel> left, right;
    int64_t ans = 0;
    for (int64_t i = 0; i < N; i++) {
      cin >> K[i] >> L[i] >> R[i];
      if (L[i] > R[i]) {
        ans += R[i];
        left.push_back(camel(K[i], L[i] - R[i]));
      } else {
        ans += L[i];
        right.push_back(camel(N - K[i], R[i] - L[i]));
      }
    }
    auto camel_comp = [](camel a, camel b) -> bool { return a.k < b.k; };
    sort(left.begin(), left.end(), camel_comp);
    sort(right.begin(), right.end(), camel_comp);
    priority_queue<int64_t, vector<int64_t>, greater<int64_t>> pq;
    int64_t tmp = 1;
    for (auto e : left) {
      pq.push(e.add);
      if (e.k < tmp) {
        pq.pop();
      }
      tmp++;
    }
    while (pq.size()) {
      ans += pq.top();
      pq.pop();
    }
    tmp = 1;
    for (auto e : right) {
      pq.push(e.add);
      if (e.k < tmp) pq.pop();
      tmp++;
    }
    while (pq.size()) {
      ans += pq.top();
      pq.pop();
    }
    cout << ans << "\n";
  }
}
