t = int(input())
for i in range(t):
    n = int(input())
    klr = [[0] + list(map(int,input().split())) for i in range(n)]
    for i in range(n):
        if klr[i][2] < klr[i][3]:
            klr[i][0] = 1
    
    klr = sorted(klr, key=lambda x:(x[0], x[1]))
    
    cnt = 0
    for i in range(n):
        if i+1 <= klr[i][1]:
            cnt += klr[i][2]
        else:
            cnt += klr[i][3]
    print(cnt)