#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << '\n';
  err(++it, args...);
}
const auto start_time = std::chrono::high_resolution_clock::now();
void timecalculater() {}
template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string& second) { return '"' + second + '"'; }
string to_string(const char* second) { return to_string((string)second); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t, q, n, m, prev, maxx, i, j, k1, k2, k3, k, x, y, flag, sum, cnt,
      ans, minn, cur;
  cin >> t;
  long long l, r;
  while (t--) {
    cin >> n;
    vector<pair<long long, long long> > fr, bk;
    ans = 0;
    for (i = 0; i < n; i++) {
      cin >> k >> l >> r;
      ans += min(l, r);
      if (l > r) {
        fr.push_back({l - r, k});
      } else if (r > l) {
        bk.push_back({r - l, -k});
      }
    }
    sort(fr.begin(), fr.end());
    reverse(fr.begin(), fr.end());
    sort(bk.begin(), bk.end());
    reverse(bk.begin(), bk.end());
    set<long long> st;
    for (i = 1; i < fr.size() + 1; i++) {
      st.insert(i);
    }
    for (auto p : fr) {
      auto it = upper_bound(st.begin(), st.end(), p.second);
      if (it == st.begin()) continue;
      it--;
      ans += p.first;
      st.erase(it);
    }
    set<long long> st2;
    for (i = n; i >= n - bk.size() + 1; i--) {
      st2.insert(i);
    }
    for (auto p : bk) {
      auto it = upper_bound(st2.begin(), st2.end(), -p.second);
      if (it == st2.end()) continue;
      ans += p.first;
      st2.erase(it);
    }
    cout << ans << '\n';
  }
  return 0;
}
