#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    int n;
    cin >> n;
    map< pair<int, int>, int> mp;
    vector< unordered_set<int> > graph(2 * n + 1);
    vector< int > v(2 * n + 1);
    for(int i = 0; i < n; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        v[a] += c;
        v[b] += c;
        graph[a].insert(b);
        graph[b].insert(a);
        mp[make_pair(a, b)] = c;
        mp[make_pair(b, a)] = c;
    }
    int ans = 0;
    for(auto& i : v) {
        ans = max(ans, i);
    }
    for(int v = 1; v <= n; ++v) {
        for(auto& to : graph[v]) {
            for(auto& th : graph[to]) {
                if(graph[v].find(th) != graph[v].end()) {
                    ans = max(ans, mp[make_pair(v, to)] + mp[make_pair(th, to)] + mp[make_pair(v, th)]);
                }
            }
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tt = 1;
    cin >> tt;
    while(tt--) {
        solve();
    }
}
