#include <bits/stdc++.h>
using namespace std;
class BIT {
 public:
  vector<int> bit;
  int M;
  BIT(int M) : bit(vector<int>(M + 1, 0)), M(M) {}
  int sum(int i) {
    if (!i) return 0;
    return bit[i] + sum(i - (i & -i));
  }
  void add(int i, int x) {
    if (i > M) return;
    bit[i] += x;
    add(i + (i & -i), x);
  }
};
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<int, pair<int, int>>> v;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      v.push_back({-l, {k, r}});
    }
    sort(v.begin(), v.end());
    BIT b(n);
    long long sum = 0;
    for (auto a : v) {
      int k = a.second.first, l = -a.first, r = a.second.second;
      if (r >= l) {
        if (b.sum(n) - b.sum(k) < n - k) {
          sum += r;
          for (int i = n; i >= 1; i--) {
            if (b.bit[i] == 0) {
              b.add(i, 1);
              break;
            }
          }
        } else {
          sum += l;
          for (int i = k; i >= 1; i--) {
            if (b.bit[i] == 0) {
              b.add(i, 1);
              break;
            }
          }
        }
        continue;
      }
      if (b.sum(k) < k) {
        sum += l;
        for (int i = k; i >= 1; i--) {
          if (b.bit[i] == 0) {
            b.add(i, 1);
            break;
          }
        }
      } else {
        sum += r;
        for (int i = n; i >= 1; i--) {
          if (b.bit[i] == 0) {
            b.add(i, 1);
            break;
          }
        }
      }
    }
    cout << sum << endl;
  }
}
