#include <bits/stdc++.h>
using namespace std;
int main() {
  int te;
  cin >> te;
  for (int w = 0; w < te; w++) {
    int n;
    cin >> n;
    vector<pair<int, pair<int, int> > > v(n);
    for (int i = 0; i < n; i++)
      cin >> v[i].first >> v[i].second.first >> v[i].second.second;
    for (int i = 0; i < n; i++) v[i].first -= 1;
    long long int sum = 0;
    vector<vector<int> > vl(n), vr(n);
    set<int> vis;
    for (int i = 0; i < n; i++) {
      int a = v[i].second.first, b = v[i].second.second, ind = v[i].first;
      if (a >= b)
        vl[ind].push_back(i);
      else {
        if (ind == n - 1) continue;
        vr[ind + 1].push_back(i);
      }
    }
    set<pair<int, int> > s1, s2;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < vl[i].size(); j++) {
        int a = v[vl[i][j]].second.first;
        if (s1.size() < i + 1)
          s1.insert({a, vl[i][j]});
        else {
          set<pair<int, int> >::iterator it = s1.begin();
          if (it->first < a) {
            s1.erase(it);
            s1.insert({a, vl[i][j]});
          }
        }
      }
    }
    for (int i = n - 1; i > -1; i--) {
      for (int j = 0; j < vr[i].size(); j++) {
        int a = v[vr[i][j]].second.second;
        if (s2.size() < n - i)
          s2.insert({a, vr[i][j]});
        else {
          set<pair<int, int> >::iterator it = s2.begin();
          if (it->first < a) {
            s2.erase(it);
            s2.insert({a, vr[i][j]});
          }
        }
      }
    }
    set<pair<int, int> >::iterator it;
    for (it = s1.begin(); it != s1.end(); it++) {
      sum += it->first;
      vis.insert(it->second);
    }
    for (it = s2.begin(); it != s2.end(); it++) {
      sum += it->first;
      vis.insert(it->second);
    }
    for (int i = 0; i < n; i++) {
      int a = v[i].second.first, b = v[i].second.second;
      if (vis.find(i) == vis.end()) sum += min(a, b);
    }
    cout << sum << endl;
  }
}
