#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll int
#define vll vector<ll>
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define fr first
#define se second
#define pb push_back
#define vvll vector<vll>
#define pll pair<ll, ll>
#define vbool vector<bool>

using namespace std;
using namespace __gnu_pbds;

vector<gp_hash_table<ll, long long>> g;
long long maxl;
vbool us;

/*void dfs(ll v, ll p = -1) {
    us[v] = 1;
    for (int i : g[v]) {
        if (p != -1 && g[p].find(i) != g[p].end()) maxl = max(maxl, (long long)(mp[{i, p}]+mp[{i, v}]+mp[{v, p}]));
        if (!us[i]) dfs(i, v);
    }
}*/

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll t;
    cin >> t;
    while (t --) {
        ll n;
        cin >> n;
        vll a(n), b(n), c(n);
        g.clear();
        g.resize(n*2+7);
        us.clear();
        us.resize(n*2+7, 0);
        vector<long long> m(n*2+7, 0);

        for (int i = 0; i < n; i ++) {
            cin >> a[i] >> b[i] >> c[i];
            if (b[i] < a[i]) swap(a[i], b[i]);
            g[a[i]][b[i]] = c[i];
            g[b[i]][a[i]] = c[i];
            m[a[i]] += (long long)c[i];
            m[b[i]] += (long long)c[i];
        }
        maxl = 0;
        for (int i = 0; i <= n*2; i ++) {
            /*if(g[i.fr].size() > 5){
                a.at(29368734687653874) = 1;
            }*/
            maxl = max(maxl, m[i]);
            //if (us[i.fr]) continue;
            //dfs(i.fr);
        }
        for (int i = 0; i < n; i ++) {
            if (g[a[i]].size() < g[b[i]].size()) {
                for (auto j : g[a[i]]) {
                    if (g[b[i]].find(j.fr) != g[b[i]].end()) {
                        maxl = max(maxl, (long long)(g[j.fr][a[i]]+g[j.fr][b[i]]+c[i]));
                    }
                }
            }
            else {
                for (auto j : g[b[i]]) {
                    if (g[a[i]].find(j.fr) != g[a[i]].end()) {
                        maxl = max(maxl, (long long)(g[j.fr][a[i]]+g[j.fr][b[i]]+c[i]));
                    }
                }
            }
        }



        cout << maxl << endl;
    }
}