#include using namespace std; const array MODS = {3960, 960, 256, 128, 64, 32, 16, 8, 4, 2, 1}; int modulo(int a, int m){ if (a < m){ return a; } else { return a % m + m; } } int modpow(int a, int b, int m){ int ans = 1; while (b > 0){ if (b % 2 == 1){ ans = modulo(ans * a, m); ans %= m; } a = modulo(a * a, m); b /= 2; } return ans; } array modpow(array a, array b){ array c; for (int i = 0; i < 10; i++){ c[i] = modpow(a[i], b[i + 1], MODS[i]); } if (a[0] == 0 && b[0] > 0){ c[10] = 0; } else { c[10] = 1; } return c; } array get(long long x){ array A; for (int i = 0; i < 11; i++){ A[i] = modulo(x, MODS[i]); } return A; } array operator +(array A, array B){ array C; for (int i = 0; i < 11; i++){ C[i] = modulo(A[i] + B[i], MODS[i]); } return C; } int main(){ long long M; cin >> M; long long D; cin >> D; long long N; cin >> N; int B; cin >> B; if (M == 0 && D == 0){ if (N % 2 == 0){ cout << 0 << endl; } else { cout << 1 << endl; } } else { array m = get(M); array d = get(D); map, int> mp; mp[m] = 0; vector> h = {m}; int t = 0; int ans; while (true){ if (t == N){ ans = m[0] % B; break; } t++; m = modpow(m + d, m); h.push_back(m); if (mp.count(m) == 1){ int t2 = mp[m]; int p = (N - t2) % (t - t2) + t2; ans = h[p][0] % B; break; } mp[m] = t; } if (ans < 10){ cout << ans << endl; } else { cout << 'A' << endl; } } }