#include <bits/stdc++.h>
using namespace std;
int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int INF = 1e9;
const long long LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const long long mod = 1000000007;
int t;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> t;
  for (int i = 0; i < (int)(t); i++) {
    int n;
    cin >> n;
    long long sum = 0;
    int c = 0;
    priority_queue<pair<int, int>> que2;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        que;
    for (int j = 0; j < (int)(n); j++) {
      int r, a, b;
      cin >> r >> a >> b;
      int d = min(a, b);
      sum += d;
      if (a > b) {
        que.push(pair<int, int>(r, a - b));
        c++;
      } else {
        que2.push(pair<int, int>(r, b - a));
      }
    }
    priority_queue<int, vector<int>, greater<int>> que3;
    for (int j = (0); j < (int)(c); j++) {
      pair<int, int> p = que.top();
      que.pop();
      if (p.first >= j + 1) {
        sum += p.second;
        que3.push(p.second);
      } else if (!que3.empty()) {
        int h = que3.top();
        if (h < p.second) {
          que3.pop();
          sum -= h;
          sum += p.second;
          que3.push(p.second);
        }
      }
    }
    priority_queue<int, vector<int>, greater<int>> que4;
    for (int j = n; j > c; j--) {
      pair<int, int> p = que2.top();
      que2.pop();
      if (p.first < j) {
        sum += p.second;
        que4.push(p.second);
      } else if (!que4.empty()) {
        int h = que4.top();
        if (h < p.second) {
          que4.pop();
          sum -= h;
          sum += p.second;
          que4.push(p.second);
        }
      }
    }
    cout << sum << endl;
  }
}
