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

#define int long long
#define mp make_pair

const int mxN = 7e5;
int t, n, a[mxN], b[mxN], c[mxN];

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

        cin >> t;
        while(t--) {
                cin >> n;
                vector<int> sz(2 * n + 1, 0);
                map<pair<int,int>, int> v;
                for(int i = 0; i < n; ++i) {
                        cin >> a[i] >> b[i] >> c[i];
                        sz[a[i]] += c[i];
                        sz[b[i]] += c[i];
                        v[mp(a[i], b[i])] += c[i];
                        v[mp(b[i], a[i])] += c[i];
                }
                int mx = 0;
                for(int i = 1; i <= 2 * n; ++i) {
                        mx = max(mx, sz[i]);
                }       
                for(int i = 0; i < n; ++i) {
                        for(int z = 1; z <= 2 * n; ++z) {
                                if(z != a[i] && z != b[i]) {
                                    mx = max(mx, v[mp(z, a[i])] + v[mp(z, b[i])] + c[i]);
                                }
                        }
                }
                cout << mx << endl;
        }
}



