N, *p = map(int, open(0).read().split()) for e in p: for x in range(1, 1000): if pow(2, x, e) == x % e: print(x) break else: print(-1)