M = int(input()) D = int(input()) N = int(input()) B = int(input()) current_mod = M % B if N == 0: final_mod = current_mod else: history = [current_mod] pos_map = {current_mod: 0} steps = 0 found_cycle = False while steps < N: steps += 1 a = (current_mod + D) % B e = current_mod if a == 0 and e == 0: new_mod = 1 % B else: new_mod = pow(a, e, B) if new_mod in pos_map: prev_step = pos_map[new_mod] cycle_length = steps - prev_step remaining = N - steps if remaining > 0: remaining_steps = remaining % cycle_length current_mod = history[prev_step + remaining_steps] steps = N # Advance steps to terminate loop else: current_mod = new_mod break else: pos_map[new_mod] = steps history.append(new_mod) current_mod = new_mod final_mod = current_mod # Convert to the last digit in base B if final_mod < 10: print(final_mod) else: print('A')