#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 7;
struct data {
  int k, l, r;
  data(int a, int b, int c) {
    k = a;
    l = b;
    r = c;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<data> left, right, mid;
    for (int i = 0; i < (int)n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l > r) {
        left.push_back(data(k, l, r));
      } else if (l < r) {
        right.push_back(data(k, l, r));
      } else {
        mid.push_back(data(k, l, r));
      }
    }
    sort(left.begin(), left.end(), [](data a, data b) { return a.k < b.k; });
    sort(right.begin(), right.end(), [](data a, data b) { return a.k < b.k; });
    long long ans = 0;
    int p = 1;
    for (int i = 0; i < (int)left.size(); i++) {
      if (left[i].k >= p) {
        ans += left[i].l;
      } else
        ans += left[i].r;
      p++;
    }
    for (int i = 0; i < (int)mid.size(); i++) {
      ans += mid[i].l;
      p++;
    }
    for (int i = 0; i < (int)right.size(); i++) {
      if (right[i].k >= p) {
        ans += right[i].l;
      } else
        ans += right[i].r;
      p++;
    }
    cout << ans << '\n';
  }
  return 0;
}
