#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
void app() {
  int t;
  cin >> t;
  for (int _t = 0; _t < (int)(t); _t++) {
    int n;
    cin >> n;
    int64_t ans = 0;
    vector<set<int64_t>> cp(n), cm(n);
    for (int i = 0; i < (int)(n); i++) {
      int k;
      int64_t l, r;
      cin >> k >> l >> r;
      k--;
      if (l > r)
        cp[k].emplace(l - r), ans += r;
      else
        cm[k].emplace(r - l), ans += l;
    }
    priority_queue<int64_t, vector<int64_t>, greater<int64_t>> sp, sm;
    for (int k = 0; k < (int)(n); k++) {
      for (int64_t c : cp[k]) {
        ans += c;
        sp.push(c);
        if (sp.size() > k + 1) {
          ans -= sp.top();
          sp.pop();
        }
      }
    }
    for (int k = (n)-1; k >= (int)(0); k--) {
      for (int64_t c : cm[k]) {
        ans += c;
        sm.push(c);
        if (sm.size() > n - 1 - k) {
          ans -= sm.top();
          sm.pop();
        }
      }
    }
    cout << ans << endl;
  }
}
int main() {
  app();
  return 0;
}
