#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct BIT {
  vector<T> bit;
  int size;
  BIT(int n) : size(n), bit(n + 1, 0) {}
  T sum(int i) {
    T s = 0;
    while (i > 0) {
      s += bit[i];
      i -= (i & (-i));
    }
    return s;
  }
  T sum(int l, int r) { return sum(r) - sum(l); }
  void add(int i, T x) {
    i++;
    while (i <= size) {
      bit[i] += x;
      i += (i & (-i));
    }
  }
};
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long int ans = 0;
    vector<vector<long long int>> vl(n + 1), vr(n + 1);
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l == r) {
        ans += l;
        continue;
      }
      if (l > r) {
        ans += r;
        vl[k - 1].push_back(l - r);
      } else {
        ans += l;
        if (k < n) vr[k].push_back(r - l);
      }
    }
    priority_queue<long long int, vector<long long int>, greater<long long int>>
        que;
    vector<long long int> sl(n + 1), sr(n + 1);
    multiset<int> st1, st2;
    long long int sum = 0;
    for (int i = 0; i < n; i++)
      for (auto x : vl[i]) st2.insert(x);
    for (int i = 0; i < n; i++) {
      for (auto x : vl[i]) que.push(x);
      while (que.size() > i + 1) {
        int y = que.top();
        que.pop();
        if (st2.find(y) != st2.end())
          st2.erase(st2.find(y));
        else
          st1.erase(st1.find(y)), sum -= y;
      }
      while (!st2.empty() && st1.size() < i + 1) {
        int y = *st2.rbegin();
        st2.erase(st2.find(y));
        st1.insert(y);
        sum += y;
      }
      sl[i + 1] = sum;
    }
    while (!que.empty()) {
      que.pop();
    }
    st1.clear();
    st2.clear();
    sum = 0;
    for (int i = 0; i < n; i++)
      for (auto x : vr[i]) st2.insert(x);
    for (int i = 0; i < n; i++) {
      for (auto x : vr[i]) que.push(x);
      while (que.size() > n - i) {
        int y = que.top();
        que.pop();
        if (st2.find(y) != st2.end())
          st2.erase(st2.find(y));
        else
          st1.erase(st1.find(y)), sum -= y;
      }
      while (!st2.empty() && st1.size() < n - i) {
        int y = *st2.rbegin();
        st2.erase(st2.find(y));
        st1.insert(y);
        sum += y;
      }
      sr[n - 1 - i] = sum;
    }
    long long int mx = 0;
    for (int i = 0; i <= n; i++) {
      mx = max(mx, sl[i] + sr[n - i]);
    }
    cout << ans + mx << endl;
  }
  return 0;
}
