#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int inf = 1e9;
int T, n, k[N], l[N], r[N];
int c[N];
bool cmp(int x, int y) { return abs(l[x] - r[x]) > abs(l[y] - r[y]); }
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    set<int> q;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &k[i], &l[i], &r[i]);
      c[i] = i;
      q.insert(i);
    }
    sort(c + 1, c + n + 1, cmp);
    long long ans = 0;
    set<pair<int, int> > st, ss;
    for (int i = 1; i <= n; i++) {
      if (l[c[i]] >= r[c[i]]) {
        while (!st.empty()) {
          pair<int, int> u = *st.begin();
          st.erase(st.begin());
          auto it = q.upper_bound(u.first);
          if (it != q.end() && *it <= u.second) {
            q.insert(u.first);
            u.first = *it;
            if (u.first < u.second) st.insert(make_pair(u.first, u.second));
            q.erase(it);
            break;
          }
        }
        auto it = q.begin();
        if (*it <= k[c[i]]) {
          ans += l[c[i]];
          if (*it < k[c[i]]) st.insert(make_pair(*it, k[c[i]]));
          q.erase(it);
        } else {
          ans += r[c[i]];
        }
      } else {
        while (!ss.empty()) {
          auto to = ss.end();
          --to;
          pair<int, int> u = *to;
          ss.erase(to);
          auto it = q.lower_bound(u.first);
          if (it != q.begin()) {
            --it;
            if (*it > u.second) {
              q.insert(u.first);
              u.first = *it;
              if (u.first > u.second) ss.insert(make_pair(u.first, u.second));
              q.erase(it);
              break;
            }
          }
        }
        auto it = q.end();
        if (it != q.begin()) --it;
        if (*it > k[c[i]]) {
          ans += r[c[i]];
          ss.insert(make_pair(*it, k[c[i]]));
          q.erase(it);
        } else {
          ans += l[c[i]];
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
