#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll solve(vector<array<int, 3>> a) {
  int n = a.size();
  vector<vector<int>> events(n + 1);
  ll res = 0;
  for (auto &el : a) {
    res += el[2];
    events[min(n, el[0])].emplace_back(el[1] - el[2]);
  }
  int cur_pos = n - 1;
  multiset<pair<int, int>> st;
  for (int i = n - 1; i >= 0; i--) {
    for (auto el : events[i + 1]) {
      st.insert({el, i + 1});
    }
    for (; cur_pos >= i && st.size(); cur_pos--) {
      res += (cur_pos < st.begin()->second) * st.begin()->first;
      st.erase(st.begin());
    }
  }
  return res;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    array<vector<array<int, 3>>, 2> want;
    for (int i = 0; i < n; i++) {
      int t, L, R;
      cin >> t >> L >> R;
      want[L >= R].push_back({L >= R ? t : n - t, max(L, R), min(L, R)});
    }
    cout << solve(want[0]) + solve(want[1]) << '\n';
  }
}
