#include <bits/stdc++.h>
using namespace std;
long long solve(int n, vector<int> k, vector<long long> l,
                vector<long long> r) {
  vector<vector<long long>> by_k(n);
  long long res = 0;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (l[i] >= r[i]) {
      ++cnt;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (l[i] >= r[i]) {
      res += r[i];
      by_k[min(cnt - 1, k[i])].push_back(l[i] - r[i]);
    }
  }
  multiset<long long> cur, rest;
  long long mx_l = 0, cur_sum = 0;
  for (int i = 0; i < cnt; ++i) {
    for (long long x : by_k[i]) {
      cur_sum += x;
      cur.insert(x);
    }
    int can = i + 1;
    while ((int)(cur).size() > can) {
      long long x = *cur.begin();
      cur_sum -= x;
      cur.erase(cur.begin());
      rest.insert(x);
    }
    while ((int)(cur).size() < can && !rest.empty()) {
      auto it = prev(rest.end());
      long long x = *it;
      cur_sum += x;
      cur.insert(x);
      rest.erase(it);
    }
    while ((int)(cur).size() == can && !rest.empty() &&
           *prev(rest.end()) > *cur.begin()) {
      long long x = *cur.begin();
      auto it = prev(rest.end());
      long long y = *it;
      cur_sum += y - x;
      cur.erase(cur.begin());
      rest.erase(it);
      rest.insert(x);
      cur.insert(y);
    }
    mx_l = max(mx_l, cur_sum);
  }
  res += mx_l;
  for (int i = 0; i < n; ++i) {
    by_k[i] = {};
  }
  for (int i = 0; i < n; ++i) {
    if (r[i] > l[i]) {
      res += l[i];
      if (k[i] == n - 1) {
        continue;
      }
      by_k[max(cnt, k[i] + 1)].push_back(r[i] - l[i]);
    }
  }
  cur = rest = {};
  cur_sum = 0;
  long long mx_r = 0;
  for (int i = n - 1; i >= cnt; --i) {
    for (long long x : by_k[i]) {
      cur_sum += x;
      cur.insert(x);
    }
    int can = n - i;
    while ((int)(cur).size() > can) {
      long long x = *cur.begin();
      cur_sum -= x;
      cur.erase(cur.begin());
      rest.insert(x);
    }
    while ((int)(cur).size() < can && !rest.empty()) {
      auto it = prev(rest.end());
      long long x = *it;
      cur_sum += x;
      cur.insert(x);
      rest.erase(it);
    }
    while ((int)(cur).size() == can && !rest.empty() &&
           *prev(rest.end()) > *cur.begin()) {
      long long x = *cur.begin();
      auto it = prev(rest.end());
      long long y = *it;
      cur_sum += y - x;
      cur.erase(cur.begin());
      rest.erase(it);
      rest.insert(x);
      cur.insert(y);
    }
    mx_r = max(mx_r, cur_sum);
  }
  res += mx_r;
  return res;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    vector<int> k(n);
    vector<long long> l(n), r(n);
    for (int i = 0; i < n; ++i) {
      scanf("%d %I64d %I64d", &k[i], &l[i], &r[i]);
      --k[i];
    }
    long long res = solve(n, k, l, r);
    printf("%I64d\n", res);
  }
  return 0;
}
