#include <bits/stdc++.h>
using namespace std;
template <class T>
void print(const T& x) {
  cout << x << endl;
}
template <class T, class... A>
void print(const T& first, const A&... rest) {
  cout << first << " ";
  print(rest...);
}
struct PreMain {
  PreMain() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} premain;
struct BIT {
  int sz;
  vector<int> tree;
  BIT(int n) {
    sz = n;
    tree.resize(n + 1);
  }
  long long sum(int i) {
    long long sm = 0;
    while (i > 0) {
      sm += tree[i];
      i -= i & -i;
    }
    return sm;
  }
  void add(int i, int x) {
    while (i <= sz) {
      tree[i] += x;
      i += i & -i;
    }
  }
};
int main() {
  int T;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    vector<pair<int, int>> inp(N);
    long long ans = 0;
    for (int(i) = 0; (i) < (int)(N); ++(i)) {
      int k, l, r;
      cin >> k >> l >> r;
      ans += r;
      inp[i] = pair<int, int>(k, l - r);
    }
    sort(inp.begin(), inp.end(), [](pair<int, int>& a, pair<int, int>& b) {
      return abs(a.second) > abs(b.second);
    });
    BIT bit1(N);
    BIT bit2(N);
    vector<int> undefined;
    for (int(i) = 0; (i) < (int)(N); ++(i)) {
      int k = inp[i].first;
      long long score = inp[i].second;
      if (score >= 0) {
        if (bit1.sum(k) < k) {
          bit1.add(k, 1);
          ans += score;
        } else {
        }
      } else {
        int k2 = N - k;
        if (bit2.sum(k2) < k2) {
          bit2.add(k2, 1);
          ans += 0;
        } else {
          ans += score;
        }
      }
    }
    print(ans);
  }
  return 0;
}
