#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
template <typename T>
struct RMQ {
  const T e = numeric_limits<T>::max();
  function<T(T, T)> fx = [](T x1, T x2) -> T { return min(x1, x2); };
  int n;
  vector<T> dat;
  RMQ(int n_) : n(), dat(n_ * 4, e) {
    int x = 1;
    while (n_ > x) {
      x *= 2;
    }
    n = x;
  }
  void set(int i, T x) { dat[i + n - 1] = x; }
  void build() {
    for (int k = n - 2; k >= 0; k--)
      dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]);
  }
  void update(int i, T x) {
    i += n - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = fx(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }
  T query(int a, int b) { return query_sub(a, b, 0, 0, n); }
  T query_sub(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) {
      return e;
    } else if (a <= l && r <= b) {
      return dat[k];
    } else {
      T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return fx(vl, vr);
    }
  }
  int find_rightest(int a, int b, T x) {
    return find_rightest_sub(a, b, x, 0, 0, n);
  }
  int find_leftest(int a, int b, T x) {
    return find_leftest_sub(a, b, x, 0, 0, n);
  }
  int find_rightest_sub(int a, int b, T x, int k, int l, int r) {
    if (dat[k] > x || r <= a || b <= l) {
      return a - 1;
    } else if (k >= n - 1) {
      return (k - (n - 1));
    } else {
      int vr = find_rightest_sub(a, b, x, 2 * k + 2, (l + r) / 2, r);
      if (vr != a - 1) {
        return vr;
      } else {
        return find_rightest_sub(a, b, x, 2 * k + 1, l, (l + r) / 2);
      }
    }
  }
  int find_leftest_sub(int a, int b, T x, int k, int l, int r) {
    if (dat[k] > x || r <= a || b <= l) {
      return b;
    } else if (k >= n - 1) {
      return (k - (n - 1));
    } else {
      int vl = find_leftest_sub(a, b, x, 2 * k + 1, l, (l + r) / 2);
      if (vl != b) {
        return vl;
      } else {
        return find_leftest_sub(a, b, x, 2 * k + 2, (l + r) / 2, r);
      }
    }
  }
};
int main() {
  int t;
  cin >> t;
  vector<ll> ans(t);
  for (int i = 0; i < (t); ++i) {
    int n;
    cin >> n;
    vector<pair<int, P>> a, b, c;
    for (int j = 0; j < (n); ++j) {
      int k, l, r;
      cin >> k >> l >> r;
      k--;
      if (l > r)
        a.push_back(make_pair(l, make_pair(k, r)));
      else if (l < r)
        b.push_back(make_pair(r, make_pair(k, l)));
      else
        c.push_back(make_pair(l, make_pair(k, r)));
    }
    sort(a.begin(), a.end(), greater<pair<int, P>>());
    sort(b.begin(), b.end(), greater<pair<int, P>>());
    int na = a.size();
    int nb = b.size();
    int nc = c.size();
    RMQ<int> rmq(n);
    for (int j = 0; j < (n); ++j) rmq.set(j, 0);
    rmq.build();
    ll sum = 0;
    for (int j = 0; j < (na); ++j) {
      int l = a[j].first;
      int k = a[j].second.first;
      int r = a[j].second.second;
      int idx = rmq.find_rightest(0, min(k + 1, na), 0);
      if (idx < 0) {
        int idx = rmq.find_rightest(0, na, 0);
        rmq.update(idx, 1);
        sum += r;
      } else {
        rmq.update(idx, 1);
        sum += l;
      }
    }
    for (int j = 0; j < (nb); ++j) {
      int r = b[j].first;
      int k = b[j].second.first;
      int l = b[j].second.second;
      int idx = rmq.find_leftest(max(k + 1, n - nb), n, 0);
      if (idx >= n) {
        int idx = rmq.find_leftest(n - nb, n, 0);
        rmq.update(idx, 1);
        sum += l;
      } else {
        rmq.update(idx, 1);
        sum += r;
      }
    }
    for (int j = 0; j < (nc); ++j) {
      int l = c[j].first;
      sum += l;
    }
    ans[i] = sum;
  }
  for (int i = 0; i < (t); ++i) cout << ans[i] << endl;
}
