// brute force import java.util.Arrays; import java.util.Scanner; class Main { public static void main(String[] args) { new Main().run(); } void run() { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long[] p = new long[N]; for (int i = 0; i < N; ++i) { p[i] = sc.nextLong(); System.out.println(brute(p[i])); } } long brute(long p) { for (long i=1,pw2=2%p,j=1;i<=1e10;++i,++j,pw2=pw2*2%p) { if(j>=p)j-=p; if (pw2==j) { return i; } } return -1; } long solve(long p) { if (p!=2) return 1-2*p+p*p; else return 2; } long pow(long a, long n, long mod) { long ret = 1; for (; n > 0; n >>= 1, a = a * a % mod) { if (n % 2 == 1) ret = ret * a % mod; } return ret; } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }