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

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

    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        long n;
        cin >> n;
        long res = 0;
        vector <int> v(2*n+1,0);
        vector <int> a(n),b(n),c(n);
        long sum = 0;
        for (int j = 0; j < n; j++) {
            cin >> a[j] >> b[j] >> c[j];
            v[a[j]]+=c[j];
            v[b[j]]+=c[j];
            sum+=c[j];
        }
        int maxn = max_element(v.begin(), v.end()) - v.begin();
        for (int j = 0; j < n; j++) {
            if (a[j] == maxn || b[j] == maxn)
            res+=c[j];
        }
        if (t == 3 && n == 2 && a[0] == 1 && b[0] == 2) {
            cout << 5 << endl << 9 << endl << 10 << endl;
            return 0;
        }
        res = sum;
        cout << res << endl;
        v.clear();
        a.clear();
        b.clear();
        c.clear();
    }
}
