#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PP = pair<int, P>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  do {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
  } while (0);
  int t;
  cin >> t;
  for (int tt = 0; tt < (t); tt++) {
    int n;
    cin >> n;
    vector<PP> v0, v1;
    int ans = 0;
    for (int i = 0; i < (n); i++) {
      int l, r, k;
      cin >> k >> l >> r;
      if (l >= r) {
        v0.push_back({k, {l, r}});
        ans += r;
      } else {
        v1.push_back({k, {l, r}});
        ans += l;
      }
    }
    priority_queue<int> q;
    for (int j = 1; j <= (n); j++) {
      for (auto x : v0) {
        if (x.first == j) {
          do {
          } while (0);
          q.push(x.second.first - x.second.second);
        }
      }
      while (q.size() > j) {
        auto x = q.top();
        do {
        } while (0);
        q.pop();
      }
    }
    while (q.size() > 0) {
      ans += q.top();
      q.pop();
    }
    for (int j = 1; j <= (n); j++) {
      for (auto x : v1) {
        if (x.first + 1 == n - j + 1) {
          do {
          } while (0);
          q.push(x.second.second - x.second.first);
        }
      }
      while (q.size() > j) {
        auto x = q.top();
        do {
        } while (0);
        q.pop();
      }
    }
    while (q.size() > 0) {
      ans += q.top();
      q.pop();
    }
    cout << ans << endl;
  }
}
