import sys input = sys.stdin.buffer.readline table_len = 5000005 table = [True] * table_len for i in range(2, table_len): if table[i]: for j in range(2*i, table_len, i): table[j] = False T = int(input()) for _ in range(T): A, P = map(int, input().split()) if table[P]: print(1) else: print(-1)