#include <bits/stdc++.h>
using namespace std;
 
using ll = int64_t;
constexpr ll mod = ll(1e9 + 7);
 
template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
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) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
// #ifdef COMPETITIVE_PROGRAMMING_LOCAL
// #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
// #else
#define dbg(...) if(0){}
// #endif

int main () {
  ios::sync_with_stdio(0); cin.tie(0);
 
  ll TT = 1;
  cin >> TT;
  vector<tuple<ll,ll,ll>> A;
  vector<vector<tuple<ll,ll,ll>>> before, after;
  vector<int> extra;
  vector<tuple<ll,ll,ll,ll>> ans;

  while (TT--) {
    ll n;
    cin >> n;
    A.resize(n);
    for (auto& [k,l,r] : A) cin >> k >> l >> r;
    dbg(A);
    extra.clear();
    before.clear();
    after.clear();
    before.resize(n+1); // need to put value [0,i] inclusive
    after.resize(n+1); // need to put value [i,n-1] inclusive
    for (int i = 0; i < n; ++i) {
      auto [k,l,r] = A[i];
      
      if (l > r) {
        before[k-1].emplace_back(l,r,i);
      } else if (l < r) {
        after[k].emplace_back(l,r,i);
      } else {
        extra.emplace_back(i);
      }
    }
    dbg(before);
    dbg(after);
    dbg(extra);

    for (int i = 0; i <= n; ++i) {
      sort(before[i].begin(), before[i].end(), [](const auto& a, const auto& b) {
                                                 int da = get<0>(a) - get<1>(a);
                                                 int db = get<0>(b) - get<1>(b);
                                                 return da > db;
                                               });
      sort(after[i].begin(), after[i].end(), [](const auto& a, const auto& b) {
                                                 int da = get<1>(a) - get<0>(a);
                                                 int db = get<1>(b) - get<0>(b);
                                                 return da > db;
                                               });
    }
    ans.clear();
    ans.resize(n, {-1,-1,-1,-1});

    for (int k = 0; k <= n; ++k) {
      for (auto [l,r,i] : before[k]) {
        int i_min = -1;
        for (int i = k; i >= 0; --i) {
          if (get<2>(ans[i]) == -1) {
            i_min = i;
            break;
          }
          if (i_min == -1 || get<3>(ans[i]) < get<3>(ans[i_min])) i_min = i;
        }
        if (i_min != -1 && (get<2>(ans[i_min]) == -1 || get<3>(ans[i_min]) < l-r)) {
          // ans[i_min] = make_tuple<ll,ll,ll,ll>(ll(l), ll(r), ll(i), ll(l-r));
          ans[i_min] = {l, r, i, l-r};
        }
      }
    }
    // move max unused before forward to lower places
    deque<int> unused;
    for (int i = 0; i < n; ++i) {
      if (get<2>(ans[i]) == -1) {
        unused.emplace_back(i);
      } else if (unused.size()) {
        ans[unused.front()] = ans[i];
        ans[i] = {-1,-1,-1,-1};
        unused.pop_front();
        unused.emplace_back(i);
      }
    }



    for (int k = n-1; k >= 0; --k) {
      for (auto [l,r,i] : after[k]) {
        int i_min = -1;
        for (int i = k; i < n; ++i) {
          if (get<2>(ans[i]) == -1) {
            i_min = i;
            break;
          }
          if (i_min == -1 || get<3>(ans[i]) < get<3>(ans[i_min])) i_min = i;
        }
        if (i_min != -1 && (get<2>(ans[i_min]) == -1 || get<3>(ans[i_min]) < r-l)) {
          ans[i_min] = {l,r,i,r-l};
        }
      }
    }
    vector<bool> used(n);
    dbg(ans);

    ll out = 0;
    for (int ii = 0; ii < n; ++ii) {
      if (get<2>(ans[ii]) == -1) continue;
      int i = get<2>(ans[ii]);
      out += max(get<1>(A[i]), get<2>(A[i]));
      used[i] = true;
    }


    for (int i = 0; i < n; ++i) {
      if (!used[i]) {
        out += min(get<1>(A[i]), get<2>(A[i]));
      }
    }

    cout << out << endl;
  }
}
