#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>

using namespace std;

#define int long long

mt19937 rnd_(12);


vector <vector <int>> g;


signed main() {

    int times;

    cin >> times;

    while (times--) {
        int n;
        cin >> n;

        vector <int> a(n);
        vector <int> b(n);
        vector <int> c(n);

        vector <int> sum(2 * n);

        for (int i = 0 ; i <n  ; i++) {
            cin >> a[i] >> b[i] >> c[i];

            a[i]--;
            b[i]--;

            sum[a[i]] += c[i];
            sum[b[i]] += c[i];
        }

        int ans = 0;

        for (int i= 0 ; i < 2* n ; i++)
            ans= max(ans , sum[i]);


        for (int i = 0 ; i < n ; i++) {
            for (int j = 0 ; j < n ;j++) {
                for (int k = 0 ; k < n ;k++) {
                    if ((a[i] == a[j]) and (b[i] == b[k]) and (b[j] == a[k])) {
                        ans = max(ans , c[i] + c[j] + c[k]);
                    }
                }
            }
        }

        cout << ans << "\n";

    }
}
