#include <bits/stdc++.h>
using namespace std;

#define int long long
#define mod 1000000007
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fst first
#define snd second
#define ff first
#define st second
#define endl "\n"
#define len(x) x.size()

void I_LOVE_PEREKOPSKA() {
    int n;
    cin>>n;
    vector<int> a(n),b(n),c(n);
    map<int, int> cnt;
    for (int i=0;i<n;i++) {
        cin>>a[i]>>b[i]>>c[i];
        cnt[a[i]]+=c[i];
        cnt[b[i]]+=c[i];
    }
    vector<pair<int, int>> h;
    for (int i=0;i<n;i++)
        for (int j=0;j<c[i];j++)
            h.push_back({a[i],b[i]});
    sort(all(h));
    int tp_c = 0;
    int mx = 0;
    int tt = 0;
    for (auto x:cnt) {
        if (x.snd > mx) {
            mx = x.snd;
            tt = x.ff;
        }
    }
    set<int> st;
    for (auto [k, m]:h) {
        if (k==tt) st.insert(m); else if (m==tt) st.insert(k);
    }
    int ans = 0;
    int fmx = 0;
    int kpc = 0;
    int kpk = 0;
    vector<int> souz(all(st));
    for (int i=0;i<len(souz);i++) {
        for (int j=0;j<len(souz);j++) {
            if (i==j) continue;
            int kk = souz[i];
            int mm = souz[j];
            int tp = -1;
            for (int x=0;x<n;x++) {
                if (a[x] == kk && b[x] == mm) {
                    tp = c[x]; break;
                }
            }
            if (tp==-1) continue;
            if (tp > fmx) {
                fmx = tp;
                kpc = kk;
                kpk = mm;
            }
        }
    }
    int mm = 0;
    for (int i=0;i<n;i++) {
        if (a[i]==tt && (b[i]==kpk || b[i]==kpc)) mm+=c[i];
    }
    cout<<max(fmx + mm, mx)<<endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int t = 1;
    cin>>t;
    while (t--) {
        I_LOVE_PEREKOPSKA();
    }
}