#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, flag = 1;
  char c = getchar();
  while ((c > '9' || c < '0') && c != '-') c = getchar();
  if (c == '-') flag = 0, c = getchar();
  while (c <= '9' && c >= '0') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return flag ? x : -x;
}
const int maxn = 2e5 + 5;
struct node {
  int L, R, K;
  bool operator<(const node x) const { return K < x.K; }
} a[maxn];
int main() {
  int T = read();
  while (T--) {
    int n = read();
    for (int i = (1); i <= (int)(n); i++)
      a[i].K = read(), a[i].L = read(), a[i].R = read();
    vector<node> x, y;
    for (int i = (1); i <= (int)(n); i++)
      if (a[i].L >= a[i].R)
        x.push_back(a[i]);
      else
        y.push_back(a[i]);
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    long long ans = 0;
    for (int i = (1); i <= (int)(n); i++) ans += min(a[i].L, a[i].R);
    priority_queue<int, vector<int>, greater<int> > Q;
    for (int i = (1); i <= (int)(x.size()); i++) {
      int add = abs(x[i - 1].L - x[i - 1].R), k = x[i - 1].K;
      if (k >= i)
        Q.push(add);
      else if (!Q.empty() && add > Q.top())
        Q.pop(), Q.push(add);
    }
    while (!Q.empty()) ans += Q.top(), Q.pop();
    for (int i = (1); i <= (int)(y.size()); i++) {
      int add = abs(y[y.size() - i].L - y[y.size() - i].R),
          k = n - y[y.size() - i].K;
      if (k >= i)
        Q.push(add);
      else if (!Q.empty() && add > Q.top())
        Q.pop(), Q.push(add);
    }
    while (!Q.empty()) ans += Q.top(), Q.pop();
    printf("%lld\n", ans);
  }
  return 0;
}
