#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long INF = 1e18 + 18;
const long long MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
istream &operator>>(istream &i, vector<T> &v) {
  for (long long j = (0); j < (v.size()); j++) i >> v[j];
  return i;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1) s << "	";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}
int solve() {
  long long n;
  cin >> n;
  vector<long long> K(n);
  vector<long long> X(n);
  vector<long long> Y(n);
  vector<pair<long long, long long>> vp(n);
  long long sum = 0;
  for (long long i = (0); i < (n); i++) {
    cin >> K[i] >> X[i] >> Y[i];
    K[i]--;
    vp[i] = make_pair(K[i], X[i] - Y[i]);
    sum += Y[i];
  };
  auto comp = [](pair<long long, long long> a, pair<long long, long long> b) {
    return abs(a.second) > abs(b.second);
  };
  sort(vp.begin(), vp.end(), comp);
  set<long long> st;
  for (long long i = (0); i < (n); i++) {
    st.insert(i);
  }
  st.insert(-1);
  st.insert(n);
  ;
  for (long long i = (0); i < (n); i++) {
    auto [ind, val] = vp[i];
    bool positive = true;
    if (val == 0) continue;
    if (i < n - 1) {
      auto [ni, nv] = vp[i + 1];
      if (nv < 0) positive = false;
    }
    if (val > 0) {
      auto itr = st.lower_bound(ind + 1);
      auto itr2 = st.begin();
      itr--;
      itr2++;
      if (positive) {
        if (*itr != -1) {
          sum += val;
          st.erase(*itr);
        } else {
        }
      } else {
        if (*itr2 <= ind) {
          sum += val;
          st.erase(*itr2);
        } else {
        }
      }
    } else {
      auto itr = st.lower_bound(ind + 1);
      auto itr2 = st.rbegin();
      itr2--;
      if (positive) {
        if (*itr2 > ind) {
          st.erase(*itr2);
        } else {
          sum += val;
        }
      } else {
        if (*itr != n) {
          st.erase(*itr);
        } else {
          sum += val;
        }
      }
    }
  }
  cout << sum << endl;
  return 0;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::setprecision(10);
  long long T;
  cin >> T;
  while (T--) {
    solve();
  }
}
