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

#define ll long long
#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<set<ll>> g;
map<ll, ll> m;
ll maxl;
vbool us;
map<pll, ll> mp;

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() || g[i].find(p) != g[i].end())) maxl = max(maxl, 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();
        m.clear();
        g.resize(n*2+7);
        us.clear();
        mp.clear();
        us.resize(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]);
            mp[{a[i], b[i]}] = c[i];
            mp[{b[i], a[i]}] = c[i];
            g[a[i]].insert(b[i]);
            g[b[i]].insert(a[i]);
            m[a[i]] += c[i];
            m[b[i]] += c[i];
        }
        maxl = 0;
        for (auto i : m) {
            maxl = max(maxl, i.se);
            if (us[i.fr]) continue;
            dfs(i.fr);
        }



        cout << maxl << endl;
    }
}