Nameless Beacon Prefixes
An exploration log contains transmissions. Transmission records a numeric beacon ID . Because different stations may assign different numeric IDs to the same underlying beacons, only the pattern of repeated IDs matters.
Two sequences and are renaming-equivalent if, for every pair of positions and satisfying ,
Thus, equal IDs must correspond to equal IDs, and different IDs must correspond to different IDs, but the numeric values themselves do not need to match.
For each query, two nonempty ranges and are given. Find the largest integer satisfying
such that the two prefixes
and
are renaming-equivalent.
Input
The first line contains two integers and (), the number of transmissions and the number of queries.
The second line contains integers (), the recorded beacon IDs.
Output
For each query, print one integer: the largest valid value of for the two specified ranges.
Samples
Sample 1
Input
12 4 4 9 4 2 9 2 7 1 7 8 1 8 1 6 7 12 1 6 2 7 3 6 8 12 5 6 11 12
Output
6 2 3 2
The first pair of ranges has the same repetition pattern for all positions. In the second pair, the first positions have the same pattern, while position is a repeat in one prefix and a new ID in the other. The remaining answers are and , respectively.