#include <bits/stdc++.h>
#define ll long long

using namespace std;

void solve() {
     int n;
    cin >> n;
    vector<vector<int>> v(n, vector<int> (3));
    for(int i=0; i<n; i++) {
        cin >> v[i][0] >> v[i][1] >> v[i][2];
    }
    sort(v.begin(), v.end());
    ll cost = 0, mx = 0;
    for(int i=0; i<n-1; i++) {
        int a = v[i][0], b = v[i][1], c = v[i][2];
        int a1 = v[+1][0], b1 = v[i+1][1], c1 = v[i+1][2];
        if(a==a1 || a==b1 || b==b1 || b==a1) {
            cost = c+c1;
        }
        mx = max(cost, mx);
    }
    cout << mx << endl;
}


int main() {
    int test;
    cin >> test;
    while(test--) {
        solve();
    }
}
