#include <bits/stdc++.h>
using namespace std;
struct ele {
  int k, l, r;
  bool operator<(const ele &other) const {
    return abs(l - r) < abs(other.l - other.r);
  }
};
bool cmp(const ele &x, const ele &y) { return x.k < y.k; }
int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<ele> big, small;
    for (int i = 0; i < n; i++) {
      ele tmp;
      cin >> tmp.k >> tmp.l >> tmp.r;
      if (tmp.l > tmp.r)
        big.push_back(tmp);
      else
        small.push_back(tmp);
    }
    sort(big.begin(), big.end(), cmp);
    set<ele> st;
    int old = 0, cnt = 0;
    long long ans = 0;
    for (ele &x : big) {
      if (x.k != old) {
        for (auto &it : st) ans += it.l;
        old = x.k;
        st.clear();
        st.insert(x);
        cnt++;
      } else {
        st.insert(x);
        if (x.k == cnt) {
          ans += st.begin()->r;
          st.erase(st.begin());
        } else
          cnt++;
      }
    }
    for (auto &it : st) ans += it.l;
    st.clear();
    sort(small.rbegin(), small.rend(), cmp);
    old = 0;
    int idx = -1;
    for (int i = 0; !~idx && i < small.size(); i++)
      if (small[i].k >= n)
        ans += small[i].l;
      else
        idx = i;
    for (; idx < small.size(); idx++) {
      if (small[idx].k != old) {
        for (auto &it : st) ans += it.r;
        old = small[idx].k;
        st.clear();
        st.insert(small[idx]);
        cnt++;
      } else {
        st.insert(small[idx]);
        if (small[idx].k == cnt) {
          ans += st.begin()->l;
          st.erase(st.begin());
        } else
          cnt++;
      }
    }
    for (auto &it : st) ans += it.r;
    cout << ans << '\n';
  }
}
