# Đọc toàn bộ các token từ input
inp = open(0).read().split()
if inp:
    t = int(inp[0])
    ptr = 1
    
    for _ in range(t):
        n = int(inp[ptr])
        ptr += 1
        
        has = set()
        total = 0
        ans = 0
        
        # Đọc n cặp số (x, y)
        for _ in range(n):
            x = int(inp[ptr])
            y = int(inp[ptr+1])
            ptr += 2
            has.add(x)
            total += y
            if x > ans:
                ans = x
                
        # Tính toán kết quả MEX tối đa
        cost = 0
        v = 0
        while True:
            c = 1 if (v in has or v == 0) else (1 << (v - 1))
            cost += c
            
            if cost <= total:
                if v + 1 > ans:
                    ans = v + 1
                v += 1
            else:
                break
                
        print(ans)
