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

void solve() {
    int n;
    cin>>n;
    vector<ll> a(n), b(n), c(n);
    vector<map<ll, ll>> grp(2 * n);
    for (int i = 0; i < n;i++) {
        cin>>a[i]>>b[i]>>c[i]; a[i]--; b[i]--;
        grp[a[i]][b[i]] += c[i];
        // grp[b[i]][a[i]] += c[i];
    }
    ll res = 0;
    int ind = 0;
    for (auto& arr : grp) {
        vector<int> nums;
        ll sm = 0;
        for (auto& [k, v] : arr) {
            nums.push_back(k);
            sm += v;
        }
        if (nums.size() > 1) {
            if (grp[nums[0]].find(nums[1]) != grp[nums[0]].end()) {
                res = max(res, sm + grp[nums[0]][nums[1]]);
            }
        }
        ind++;
    }
    for (int i = 0; i < n; i++) {
        grp[b[i]][a[i]] += c[i];
    }
    for (auto& arr : grp) {
        ll cr = 0;
        for (auto& [k, v] : arr) {
            cr += v;
        }
        res = max(res, cr);
    }
    cout << res << endl;
}

int main() {
    // freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;
    while (t) {
        solve();
        t--;
    }
}