mod = 17 for _ in range(int(input())): A, B = map(int, input().split()) print(B * (B + 1) * pow(2, mod - 2, mod) % mod)