
#include <bits/stdc++.h>
#pragma GCC optimize("O3")

using namespace std;
typedef long long ll;
const ll dim=1400007;
ll n,a[dim],T[dim],b[dim], c[dim], used[dim],ma, G[dim];
string s;
set<ll>st[dim];
map<pair<ll,ll>,ll>mp;
/*ll build(int v, int tl, int tr){
   if(tl==tr){
        T[v]=a[tl];
        return T[v];
   }
   int tm=(tl+tr)>>1;

    T[v]=build(2*v+1, tl, tm)+build(2*v+2, tm+1, tr);
}

ll get(int v, int tl, int tr, int l, int r)
void solve(){
    cin>>n;
}*/
void solve(){
    cin>>n;
     ll ma=0;


     for(int i=1; i<=2*n; i++){G[i]=0;}
    for(int i=1; i<=n; i++){cin>>a[i]>>b[i]>>c[i]; ma=max(ma, c[i]);
     G[a[i]]+=c[i];
     G[b[i]]+=c[i];
     //st[a[i]].insert(G[a[i]]);

    }

    for(int i=1; i<=2*n; i++)ma=max(ma, G[i]);

   for(int i=1; i<=n; i++){
      mp[{a[i], b[i]}]=c[i];
   }


   for(ll i=1; i<=n; i++){
       for(ll j=1; j<=2*n; j++){

        if(a[i]==j || b[i]==j)continue;
         ll p1=min(a[i], j);
         ll p2=max(a[i], j);
         ll p3=min(b[i], j);
         ll p4=max(b[i], j);

        if(mp[{p1,p2}]!=0 && mp[{p3,p4}]!=0){
            ma=max(c[i]+mp[{p1,p2}]+mp[{p3,p4}], ma); //cout << "TEST: " << i << " " << p1 << " " << p2 << " " << p3 << " " << p4 << " " << mp[{p1,p2}]+mp[{p3,p4}] << endl;
        }
       }
   }

    cout << ma << endl;
}
int main()
{

    int t;
    //t=1;
    cin>>t;
    while(t--)solve();

    return 0;
}
