#include <bits/stdc++.h>

using namespace std;

#define ll long long
//#define int long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
using pii = pair<int, int>;

struct ss {
    int a, b, c;
};

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

    ll t, n, a, b, c, ans, k;
    cin >> t;

    while(t--) {
        cin >> n;

        vector<ss> v(n);
        vector<ll> sum(n + n, 0);
        map<pii, int> mp;
        ans = 0;

        for(int i = 0; i < n; i++) {
            cin >> a >> b >> v[i].c;
            a--;
            b--;
            v[i].a = min(a, b);
            v[i].b = max(a, b);
            sum[a] += v[i].c;
            sum[b] += v[i].c;
            mp[{a, b}] = i;
        }

        /*for(int i: sum) {
            cout << i << " ";
        }
        cout << "\n";*/

        ans = *max_element(all(sum));

        if(n <= 2) {
            cout << ans << "\n";
            continue;
        }

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(i == j) {
                    continue;
                }

                k = mp[{v[i].a, v[j].b}];

                if(i == k || j == k) {
                    continue;
                }

                if(v[i].b == v[j].a && v[k].a == v[i].a && v[k].b == v[j].b) {
                    ans = max(ans, 0LL + v[i].c + v[j].c + v[k].c);
                }
            }
        }

        cout << ans << "\n";
    }

    return 0;
}
// :)
