The great detective Sherlock Holmes has discovered a peculiar cipher machine at the scene of a mysterious crime. The machine contains a double-ended sequence holder with n
number cards, where the i
-th card shows the number \( a_i \).
Holmes has deduced that the machine operates on a specific algorithm:
- In each operation cycle, the machine extracts the first two leftmost cards \( A \) and \( B \).
- If \( A > B \), it places \( A \) at the front and \( B \) at the back.
- Otherwise, it places \( B \) at the front and \( A \) at the back.
Holmes finds a ledger with \( q \) timestamps. Each timestamp \( m_j \) represents a critical moment in the criminal's plan. To decode the hidden message, Holmes must determine which two cards would be extracted at the start of the \( m_j \)-th operation cycle.
The first line contains two integers \( n \) and \( q \) \((2 \leq n \leq 10^5, \ 0 \leq q \leq 3 \times 10^5)\) — the number of cards in the sequence and the number of timestamps.
The second line contains \( n \) integers \( a_1, a_2, \ldots, a_n \) \(\left(0 \leq a_i \leq 10^9\right)\) — the initial card sequence.
The next \( q \) lines contain one integer each, \( m_j \) \(\left(1 \leq m_j \leq 10^{18}\right)\).
For each timestamp, output two numbers \( A \) and \( B \) — the numbers on the cards that would be extracted at the start of the \( m_j \)-th operation cycle.