#include using namespace std; typedef long long ll; typedef pair pll; typedef pair pii; typedef pair pdd; typedef vector vi; typedef vector vll; typedef vector vd; typedef vector vvi; typedef vector vvll; typedef vector vpii; typedef vector vpll; typedef vector vpdd; typedef vector vvd; #define yn(ans) printf("%s\n", (ans)?"Yes":"No"); #define YN(ans) printf("%s\n", (ans)?"YES":"NO"); template bool chmax(T &a, T b) { if (a >= b) return false; a = b; return true; } template bool chmin(T &a, T b) { if (a <= b) return false; a = b; return true; } #define FOR(i, s, e, t) for ((i) = (s); (i) < (e); (i) += (t)) #define REP(i, e) for (int i = 0; i < (e); ++i) #define REP1(i, s, e) for (int i = (s); i < (e); ++i) #define RREP(i, e) for (int i = (e); i >= 0; --i) #define RREP1(i, e, s) for (int i = (e); i >= (s); --i) #define all(v) v.begin(), v.end() #define pb push_back #define qb pop_back #define pf push_front #define qf pop_front #define maxe max_element #define mine min_element ll inf = 1e18; #define DEBUG printf("%d\n", __LINE__); fflush(stdout); template void print(vector &v, bool withSize) { if (withSize) cout << v.size() << endl; REP(i, v.size()) cout << v[i] << " "; cout << "\n"; } mt19937_64 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count()); int __FAST_IO__ = []() { std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); return 0; }(); int main() { ll M, D, N; int B; cin >> M >> D >> N >> B; if (N == 0) { int ans = D % B; if (ans == 10) printf("A\n"); else printf("%d\n", ans); return 0; } vvi p(B); vi sc(B), tc(B); REP(i, B) { vi used(B, -1); int c = 1, id = 0; while (used[c] == -1) { used[c] = id; p[i].pb(c); int next = c * i % B; id++; c = next; } sc[i] = used[c], tc[i] = id - used[c]; // cout << i << " " << sc[i] << " " << tc[i] << endl; // print(p[i], false); } int x = D % B; vi used(B, -1); vi q; int ss = -1, tt = -1; int ans = -1; REP(i, N) { // printf("%d %d\n", i, x); if (used[x] != -1) { ss = used[x], tt = i - ss; //// printf("%d %d\n", ss, tt); // print(used, false); // print(q, false); ans = q[ss + ((N - ss) % tt)]; break; } else { used[x] = i; q.pb(x); x = (x + M) % B; if (M < sc[x]) x = p[x][M]; else { x = p[x][((M - sc[x]) % tc[x]) + sc[x]]; } } //printf("after %d %d\n", i, x); } if (ans == -1) ans = x; if (ans == 10) printf("A\n"); else printf("%d\n", ans); return 0; }