# url = 'https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRFInKex8DFAUo'

tc = int(input())
for _ in range(tc):
    n = int(input())
    atoms = []
    mt, mr, mb, ml = 0, 0, 0, 0
    for i in range(n):
        x, y, d, e = map(int, input().split())
        if x > mr:
            mr = x
        if x < ml:
            ml = x
        if y > mt:
            mt = y
        if y < mb:
            mb = y
        atoms.append([x*2, y*2, d, e])
    mt *= 2
    mr *= 2
    mb *= 2
    ml *= 2
    direction = {0: (0, 1), 1: (0, -1), 2: (-1, 0), 3: (1, 0)}
    answer = 0
    # print(atoms)
    while atoms:
        cnt = len(atoms)-1
        collapse = {}
        while cnt >= 0:
            x, y, d, e = atoms[cnt]
            nx, ny = x+direction[d][0], y+direction[d][1]
            if not ml <= nx <= mr or not mb <= ny <= mt:
                atoms.pop(cnt)
            else:
                loc = '{} {}'.format(nx, ny)
                atoms[cnt] = [nx, ny, d, e]
                if collapse.get(loc):
                    collapse[loc].append(atoms[cnt])
                else:
                    collapse[loc] = [atoms[cnt]]
            cnt -= 1
        # print(atoms)
        for col in collapse:
            if len(collapse[col]) > 1:
                print(collapse[col])
                for i in collapse[col]:
                    atoms.remove(i)
                    answer += i[3]
    print('#{} {}'.format(_+1, answer))

        

