#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC optimize("tree-vectorize")
#pragma GCC target("sse4")
using namespace std;
using lint = long long;
using pii = pair<int, int>;
using pll = pair<lint, lint>;
template <typename T>
using vc = vector<T>;
template <typename T>
using vvc = vector<vector<T>>;
template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
constexpr lint ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }
template <class T>
class BinaryIndexedTree {
 private:
  int n;
  vector<T> bit;
  T _sum(int i) {
    T res = 0;
    while (i) {
      res += bit[i];
      i -= i & -i;
    }
    return res;
  }
  void _add(int i, T x) {
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }

 public:
  T sum(int i) { return _sum(i + 1); }
  void add(int i, T x) { _add(i + 1, x); }
  BinaryIndexedTree() : n(0) {}
  explicit BinaryIndexedTree(size_t _n, T _def = 0) : n(_n) {
    bit.resize(n + 1, _def);
  }
};
class ECamelTrain {
 public:
  static bool cmp(pair<int, pll>& p1, pair<int, pll>& p2) {
    return p1.second.first - p1.second.second <
           p2.second.first - p2.second.second;
  }
  void solve(std::istream& in, std::ostream& out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr), out.tie(nullptr);
    int tt;
    in >> tt;
    while (tt--) {
      int N;
      in >> N;
      vc<pair<int, pll>> L, R;
      for (int i = (0); i < (N); ++i) {
        int k, l, r;
        in >> k >> l >> r;
        if (l >= r)
          L.emplace_back(k - 1, pll(l, r));
        else
          R.emplace_back(N - k - 1, pll(l, r));
      }
      BinaryIndexedTree<int> B1(N), B2(N);
      sort((L).rbegin(), (L).rend());
      sort((R).rbegin(), (R).rend());
      lint ans = 0;
      for (auto& p : L) {
        int k = p.first, l = p.second.first, r = p.second.second;
        if (B1.sum(k) <= k) {
          B1.add(k, 1);
          ans += l;
        } else
          ans += r;
      }
      for (auto& p : R) {
        int k = p.first, l = p.second.first, r = p.second.second;
        if (0 <= k and B2.sum(k) <= k) {
          B2.add(k, 1);
          ans += r;
        } else
          ans += l;
      }
      out << ans << '\n';
    }
  }
};
int main() {
  ECamelTrain solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
