#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll DIM = 2*7e5+7;
vector<pll> Q[DIM];
pll a[DIM];
ll c[DIM], d[DIM];
map<ll, ll> m[DIM];
void solve(){
    ll n;
    cin >> n;
    for(int i=1; i<=2*n; i++){
        d[i] = 0;
        Q[i].clear();
        m[i].clear();
    }

    ll ans = 0;
    for(int i=1; i<=n; i++){
        cin >> a[i].first >> a[i].second >> c[i];
        m[a[i].first][a[i].second] = c[i];
    }
    sort(a+1, a+1+n);
    for(int i=1; i<=n; i++){
        ll target = 0;
        for(auto x : Q[a[i].first]){
            target = x.first;
            ll id = x.second;
            //cout << id << " " << a[i].second << endl;
            //cout << i << " " << m[1][4] << " " << target << " " << c[i] << endl;
            ans = max(ans, target + c[i] + m[id][a[i].second]);
        }
        Q[a[i].second].push_back({c[i], a[i].first});
    }
    cout << ans << endl;
}
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}