#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
  return os << "(" << x.first << "," << x.second << ")";
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    vector<pair<int, int>> vec;
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      scanf("%d%d%d", &k, &l, &r);
      if (l > r) {
        vec.push_back({l - r, k});
        ans += r;
      } else {
        vec.push_back({r - l, -(n - k)});
        ans += l;
      }
    }
    sort(vec.rbegin(), vec.rend());
    auto f = [&](int m) {
      set<int> empty;
      for (int i = 1; i <= n; ++i) empty.insert(i);
      long long score = 0;
      for (int i = 0; i < n; ++i) {
        int k = vec[i].second;
        if (k < 0) {
          auto it = empty.lower_bound(max(m + 1, n + k + 1));
          if (it == empty.end()) continue;
          empty.erase(it);
          score += vec[i].first;
        } else {
          auto it = empty.upper_bound(min(m, k));
          if (it == empty.begin()) continue;
          --it;
          empty.erase(it);
          score += vec[i].first;
        }
      }
      return score;
    };
    int lo = 0, hi = n;
    while (lo < hi) {
      int m1 = (lo + hi) / 2;
      int m2 = m1 + 1;
      if (f(m1) < f(m2))
        lo = m2;
      else
        hi = m1;
    }
    ans += f(lo);
    printf("%lld\n", ans);
  }
}
