#include #include using namespace std; int power(long long e, int m) { assert (m % 2 == 1); long long ans = 1, p = 2; while (e > 0) { if (e & 1) ans = ans * p % m; p = p * p % m; e /= 2; } return ans; } int totient(int n) { int ans = 1; for (int i = 2; i * i <= n; i++) { int q = 1; while (n % i == 0) { n /= i; q *= i; } if (q > 1) ans *= q / i * (i-1); } if (n > 1) ans *= n-1; return ans; } int tower(int n, int m) { int r = 1, e = 0; while (m % 2 == 0) { r *= 2; e++; m /= 2; } if (m == 1) return 0; int t = totient(m); return power(tower(n-1, t) + t - e, m) * r; } long long solve(int p) { if (p == 2) return 2; long long a = tower(50, p), b = tower(50, p-1); if (a > b) b += p-1; return a + (b-a) * p; } int main() { int n; cin >> n; while (n--) { int p; cin >> p; long long ans = solve(p); assert (p == 2 || power(ans, p) % p == ans % p); cout << ans << endl; } }