#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    []() {
      int n;
      cin >> n;
      vector<int> K(n), L(n), R(n);
      for (int i = 0; i < n; i++) {
        cin >> K[i] >> L[i] >> R[i];
        K[i]--;
      }
      vector<vector<int>> LL(n), RR(n + 1);
      int64_t ans = 0;
      for (int i = 0; i < n; i++) {
        if (L[i] > R[i])
          LL[K[i]].push_back(L[i] - R[i]);
        else
          RR[K[i] + 1].push_back(R[i] - L[i]);
        ans += max(L[i], R[i]);
      }
      int cur = -1;
      for (int i = 0; i < n; i++) {
        if (LL[i].size() == 0) continue;
        sort(LL[i].begin(), LL[i].end());
        int cap = min(int(LL[i].size()), i - cur);
        ans -= accumulate(LL[i].begin(), LL[i].end() - cap, 0L);
        cur += cap;
      }
      cur = n;
      for (int i = n; i >= 0; i--) {
        if (RR[i].size() == 0) continue;
        sort(RR[i].begin(), RR[i].end());
        int cap = min(int(RR[i].size()), cur - i);
        ans -= accumulate(RR[i].begin(), RR[i].end() - cap, 0L);
        cur -= cap;
      }
      cout << ans << endl;
    }();
  }
}
