#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000001;
struct SegmentTree {
  vector<int> val;
  vector<int> mn;

 public:
  SegmentTree(int N) : val(N * 4 + 5), mn(N * 4 + 5) {}
  void update(int l, int r, int x, int p, int v) {
    if (l == r) {
      val[l] = v;
      mn[x] = l;
      return;
    }
    int mid = l + r >> 1;
    if (p <= mid) {
      update(l, mid, x << 1, p, v);
    } else {
      update(mid + 1, r, x << 1 | 1, p, v);
    }
    if (val[mn[x << 1]] <= val[mn[x << 1 | 1]]) {
      mn[x] = mn[x << 1];
    } else {
      mn[x] = mn[x << 1 | 1];
    }
  }
  int query(int l, int r, int x, int a, int b) {
    if (l > b || r < a) {
      return -1;
    }
    if (l >= a && r <= b) {
      return mn[x];
    }
    int mid = l + r >> 1;
    int L = query(l, mid, x << 1, a, b);
    int R = query(mid + 1, r, x << 1 | 1, a, b);
    if (L == -1) {
      return R;
    }
    if (R == -1) {
      return L;
    }
    if (val[L] < val[R]) {
      return L;
    } else {
      return R;
    }
  }
  int V(int p) { return val[p]; }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<int> k(n);
    vector<int> l(n);
    vector<int> r(n);
    vector<tuple<int, int, int> > a;
    vector<tuple<int, int, int> > b;
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      if (k == n) {
        ans += l;
      } else if (l >= r) {
        a.emplace_back(k, l, r);
      } else {
        b.emplace_back(n - k, r, l);
      }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    auto solve = [&](const vector<tuple<int, int, int> > a) {
      int n = a.size();
      SegmentTree seg(n + 1);
      long long ans = 0;
      for (int i = 0; i < n; ++i) {
        auto [k, l, r] = a[i];
        if (k > i) {
          ans += l;
          seg.update(1, n, 1, i + 1, l - r);
        } else {
          int pos = seg.query(1, n, 1, 1, k);
          if (l - r > seg.V(pos)) {
            ans -= seg.V(pos);
            ans += l;
            seg.update(1, n, 1, pos, l - r);
          } else {
            ans += r;
          }
        }
      }
      return ans;
    };
    cout << ans + solve(a) + solve(b) << '\n';
  }
  return 0;
}
