#include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char* _delim = " ") {
  for (Tp current = _begin; current != _end; ++current) {
    std::cout << *current << _delim;
  }
  std::cout << '\n';
}
using ll = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);
struct Camel {
  int K;
  int L;
  int R;
};
bool operator<(const Camel& lhs, const Camel& rhs) {
  if (abs(lhs.R - lhs.L) == abs(rhs.R - rhs.L)) {
    return lhs.K < rhs.K;
  }
  return abs(lhs.R - lhs.L) > abs(rhs.R - rhs.L);
}
class SegmentTree {
 public:
  struct Node {
    int Sum = 0;
    int Val;
  };
  explicit SegmentTree(int n) : tree(n << 2), size(n) { Build(0, 0, size - 1); }
  void Set(int i, int x) { UpdateSingleImpl(0, 0, size - 1, i, x); }
  int Min() const { return tree[0].Val; }
  int Query(int l, int r) { return QueryRangeImpl(0, 0, size - 1, l, r); }

 private:
  int Left(int v) const { return (v << 1) + 1; }
  int Right(int v) const { return (v << 1) + 2; }
  void UpdateVertex(int v) {
    tree[v].Val = min(tree[Left(v)].Val, tree[Right(v)].Val);
    tree[v].Sum = tree[Left(v)].Sum + tree[Right(v)].Sum;
  }
  void Build(int v, int tl, int tr) {
    if (tl == tr) {
      tree[v].Val = -(tl + 1);
      return;
    }
    const int mid = (tl + tr) >> 1;
    Build(Left(v), tl, mid);
    Build(Right(v), mid + 1, tr);
    UpdateVertex(v);
  }
  void UpdateSingleImpl(int v, int tl, int tr, int i, int x) {
    if (tl == tr) {
      tree[v].Val += x;
      tree[v].Sum += x;
      return;
    }
    const int mid = (tl + tr) >> 1;
    if (i <= mid) {
      UpdateSingleImpl(Left(v), tl, mid, i, x);
    } else {
      UpdateSingleImpl(Right(v), mid + 1, tr, i, x);
    }
    UpdateVertex(v);
  }
  int QueryRangeImpl(int v, int tl, int tr, int l, int r) {
    if (l > r) {
      return 0;
    }
    if (tl == l && r == tr) {
      return tree[v].Sum;
    }
    const int mid = (tl + tr) >> 1;
    return QueryRangeImpl(Left(v), tl, mid, l, min(mid, r)) +
           QueryRangeImpl(Right(v), mid + 1, tr, max(l, mid + 1), r);
  }
  vector<Node> tree;
  const int size;
};
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int test_cases;
  cin >> test_cases;
  while (test_cases--) {
    int n;
    cin >> n;
    vector<Camel> arr(n);
    for (int i = 0; i < (n); ++i) {
      cin >> arr[i].K >> arr[i].L >> arr[i].R;
      --arr[i].K;
    }
    sort((arr).begin(), (arr).end());
    SegmentTree st(n);
    ll ans = 0;
    int tot = 0;
    for (const auto& c : arr) {
      if (c.L > c.R || c.K == n - 1) {
        st.Set(c.K, 1);
        if (st.Min() > 0) {
          st.Set(c.K, -1);
          st.Set(n - 1, 1);
          ans += c.R;
        } else {
          ans += c.L;
        }
      } else {
        int cnt = st.Query(0, c.K);
        if (tot - cnt - (c.K + 1 - cnt) < n - 1 - c.K) {
          st.Set(n - 1, 1);
          ans += c.R;
        } else {
          st.Set(c.K, 1);
          ans += c.L;
        }
      }
      ++tot;
    }
    cout << ans << "\n";
  }
  return 0;
}
