#include <bits/stdc++.h>
using namespace std;
long long camel(int n, vector<vector<int>> aklr) {
  long long ans = 0;
  vector<int> happy(n, 0);
  for (int i = 0; i < n; i++) {
    if (aklr.at(2) < aklr.at(3)) {
      int pt = aklr.at(i).at(1);
      bool ed = false;
      while (pt < n) {
        if (happy.at(pt) == 0) {
          happy.at(pt) = i + 1;
          ans += aklr.at(i).at(3);
          ed = true;
          break;
        }
        pt++;
      }
      if (ed) continue;
    } else if (aklr.at(2) > aklr.at(3)) {
      int pt = aklr.at(i).at(1);
      bool ed = false;
      while (pt >= 0) {
        if (happy.at(pt) == 0) {
          happy.at(pt) = i + 1;
          ans += aklr.at(i).at(3);
          ed = true;
          break;
        }
        pt--;
      }
      if (ed) continue;
    }
    ans += min(aklr.at(i).at(2), aklr.at(i).at(3));
  }
  return ans;
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;
    vector<vector<int>> aklr(n, vector<int>(4));
    for (int j = 0; j < n; j++) {
      cin >> aklr.at(j).at(1) >> aklr.at(j).at(2) >> aklr.at(j).at(3);
      aklr.at(j).at(0) = abs(aklr.at(j).at(2) - aklr.at(j).at(3));
    }
    sort(aklr.begin(), aklr.end());
    reverse(aklr.begin(), aklr.end());
    long long ans = camel(n, aklr);
    cout << ans << endl;
  }
}
