#include <bits/stdc++.h>
using namespace std;
const int M = 200010;
int T, m, ans = 0, tot1 = 0, tot2 = 0;
template <typename T>
inline void read(T &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-') f = -1;
  for (; ch <= '9' && ch >= '0'; ch = getchar()) x = x * 10 + ch - '0';
  x *= f;
}
struct query {
  int w, k;
  inline bool operator<(const query &x) const { return k < x.k; }
} q1[M], q2[M];
priority_queue<int, vector<int>, greater<int> > q;
inline void solve() {
  read(m);
  ans = tot1 = tot2 = 0;
  for (register int i = 1, k, l, r; i <= m; i++) {
    read(k);
    read(l);
    read(r);
    if (l > r)
      ans += r, q1[++tot1] = (query){l - r, k};
    else
      ans += l, q2[++tot2] = (query){r - l, m - k};
  }
  sort(q1 + 1, q1 + tot1 + 1);
  sort(q2 + 1, q2 + tot2 + 1);
  for (register int i = 1; i <= tot1; i++)
    if (q1[i].k)
      if (q.size() < q1[i].k)
        q.push(q1[i].w);
      else if (q.size() == q1[i].k && q1[i].w > q.top())
        q.pop(), q.push(q1[i].w);
  for (; !q.empty(); q.pop()) ans += q.top();
  for (register int i = 1; i <= tot2; i++)
    if (q2[i].k)
      if (q.size() < q2[i].k)
        q.push(q2[i].w);
      else if (q.size() == q2[i].k && q2[i].w > q.top())
        q.pop(), q.push(q2[i].w);
  for (; !q.empty(); q.pop()) ans += q.top();
  printf("%d\n", ans);
}
int main() {
  read(T);
  while (T--) solve();
  return 0;
}
