#include <bits/stdc++.h>
using namespace std;
struct rakuda {
  int l;
  int r;
};
int main() {
  int t;
  cin >> t;
  for (int _ = 0; _ < t; _++) {
    int n;
    cin >> n;
    vector<vector<vector<rakuda>>> camel(n, vector<vector<rakuda>>(2));
    priority_queue<int> que, que2;
    int k, l, r;
    for (int i = 0; i < n; i++) {
      cin >> k >> l >> r;
      k--;
      camel[k][l >= r].push_back((rakuda){l, r});
    }
    int margin[2] = {0, 0};
    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (rakuda temp : camel[i][1]) que.push(temp.r - temp.l);
      for (rakuda temp : camel[i][1]) ans += temp.l;
      margin[1] = margin[1] + 1 - camel[i][1].size();
      if (margin[1] < 0) {
        for (int __ = 0; __ < abs(margin[1]); __++) {
          ans += que.top();
          que.pop();
        }
        margin[1] = 0;
      }
      for (rakuda temp : camel[n - (i + 1)][0]) que2.push(temp.l - temp.r);
      for (rakuda temp : camel[n - (i + 1)][0]) ans += temp.r;
      margin[0] = margin[0] + 1 - camel[n - (i + 1)][0].size();
      if (i == 0) {
        margin[0] = 1;
        for (rakuda temp : camel[n - (i + 1)][0]) {
          ans += que2.top();
          que2.pop();
        }
      }
      if (margin[0] < 0) {
        for (int __ = 0; __ < abs(margin[0]); __++) {
          ans += que2.top();
          que2.pop();
        }
        margin[0] = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
