c = int(input())
res = []
for i in range(c):
    setx = set()
    n = int(input())
    suma = [[0, 0, 0, 0] for _ in range(n * 2)]
    sumax = [0] * (n * 2)
    a = [list(map(int, input().split())) for _ in range(n)]
    a.sort(key=lambda x:x[2], reverse=True)
    for h in range(n):
        x = a[h][0] - 1
        y = a[h][1] - 1
        setx.add(x)
        setx.add(y)
        if suma[x][1] < 2:
            suma[x][0] += a[h][2]
            suma[x][1] += 1
        sumax[x] += a[h][2]

        if suma[x][1] == 1:
            suma[x][2] = a[h][1]
        if suma[x][1] == 2:
            suma[x][3] = a[h][1]
            suma[x][1] += 1
        if suma[y][1] < 2:
            suma[y][0] += a[h][2]
            suma[y][1] += 1
        sumax[y] += a[h][2]


        if suma[y][1] == 1:
            suma[y][2] = a[h][0]
        if suma[y][1] == 2:
            suma[y][3] = a[h][0]
            suma[y][1] += 1
        
    
    for h in setx:
        f = [0]
        for j in a:
            if (j[0] == suma[h][2] and j[1] == suma[h][3]) or (j[1] == suma[h][2] and j[0] == suma[h][3]):
                suma[h][0] += j[2]
                break

    res.append(max(max(suma)[0], max(sumax)))
for i in range(c):
    print(res[i])