import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Task1 {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BoyType[][] boyTypes = null;
        try {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int T = Integer.parseInt(st.nextToken());
            boyTypes = new BoyType[T][];
            for (int i = 0; i < T; i++) {
                st = new StringTokenizer(br.readLine());
                boyTypes[i] = new BoyType[Integer.parseInt(st.nextToken())];
                for (int j = 0; j < boyTypes[i].length; j++) {
                    st = new StringTokenizer(br.readLine());
                    boyTypes[i][j] = new BoyType(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
                }
            }
        } catch (IOException ex) {}
        long max = -1;
        long curCount = 0;
        List<BoyType> cur = new ArrayList<>();
        for (int t = 0; t < boyTypes.length; t++) {
            for(int i = 0; i < boyTypes[t].length; i++) {
                cur.add(boyTypes[t][i]);
                curCount += boyTypes[t][i].c;
                for(int shift = 0; shift < boyTypes[t].length - 1; shift++) {
                    for (int j = shift; j < boyTypes[t].length; j++) {
                        if(i == j) continue;
                        if (check(cur, boyTypes[t][j])) {
                            cur.add(boyTypes[t][j]);
                            curCount += boyTypes[t][j].c;
                        }
                    }
                    if(curCount > max) {
                        max = curCount;
                    }
                    curCount = 0;
                    cur.clear();
                }
            }
            System.out.println(max);
            max = 0;
        }
    }

    private static boolean check(List<BoyType> cur, BoyType another) {
        for(BoyType curType : cur) {
            if(!curType.checkForAnotherType(another)) {
                return false;
            }
        }
        return true;
    }
}


/*
1
1
1 6123 3

 */
/*
10
5
1 2 3
2 3 5
3 4 6
4 1 5
1 3 5
5
1 2 3
2 3 5
3 4 3
4 1 5
1 3 5
5
1 2 3
2 3 5
3 4 3
4 1 5
1 3 5
5
1 2 3
2 3 5
3 4 3
4 1 5
1 3 5
5
1 2 3
2 3 5
3 4 3
4 1 5
1 3 5
5
1 2 3
2 3 5
3 4 3
4 1 5
1 3 5
5
1 2 3
2 3 5
3 4 3
4 1 5
1 3 5
5
1 2 3
2 3 5
3 4 3
4 1 5
1 3 5
5
1 2 3
2 3 5
3 4 3
4 1 5
1 3 5
5
1 2 3
2 3 5
3 4 3
4 1 5
1 3 5



 */
class BoyType {
    int a;
    int b;
    long c;

    public BoyType(int a, int b, long c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public boolean checkForAnotherType(BoyType another) {
        return this.a == another.a || this.b == another.a || this.a == another.b || this.b == another.b;
    }

    public String toString() {
        return "BoyType: a: " + a + "; b: " + b + "; c: " + c;
    }
}
