GCD requests#10470

GCD requests

4 seconds
1,024 megabytes
Easy

You are given an array of non-negative integers of length and queries. Each query consists of two numbers and . For each query, determine the maximum of the greatest common divisors (GCD) of all pairs in the subarray from to . Specifically, find:

\smash{\displaystyle\max_{l \leq i < j \leq r}} \gcd(a_i, a_j)

Input

The first line contains a single integer (2 \le n \le 2 \cdot 10^5 ) — the size of the array.

The second line contains integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^6) — the elements of the array.

The third line contains a single integer (1 \le m \le 2 \cdot 10^5 ) — the number of queries.

Each of the next lines contains two integers l_i, r_i (1 \le l_i < r_i \le n) — the boundaries of the query.

Output

For each query, output a single integer — the answer to that query.

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Note

\gcd(a, b) denotes the greatest common divisor of numbers and .

Consider the second example:

  • In the first four queries, the segment consists of only two numbers, so the answer is their greatest common divisor.

  • Query : the greatest common divisor is for numbers a_1 and a_4, \gcd(a_1, a_4)=\gcd(10, 50)=10.

  • Query : calculate \max(\gcd(a_3, a_4), \gcd(a_3, a_5), \gcd(a_4, a_5))=\max(2, 3, 1) = 3.

  • Query : calculate \max(\gcd(a_1, a_2), \gcd(a_1, a_3), \gcd(a_2, a_3))=\max(1, 2, 3) = 3.

Scoring

  1. ( points): m, n, a_i \leq 200;

  2. ( points): m, n \leq 200;

  3. ( points): a_i \leq 50;

  4. ( points): all a_i are powers of two;

  5. ( points): m, n \leq 15000;

  6. ( points): m, n \leq 35000;

  7. ( points): no additional constraints.