PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000145. Truy vấn GCD sau khi xóa đoạn (GCD Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn GCD sau khi xóa đoạn (GCD Queries)

GCD Queries

Source: CodeChef

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence AA of NN positive integers indexed from 11 to NN. For a query (L,R)(L,R), imagine removing every element from index LL through RR, inclusive.

Find the GCD of all remaining elements. Every query is independent and uses the original sequence. The input guarantees that at least one element remains after the removal.

Input

The first line contains N,QN,Q, followed by NN values AiA_i. Each of the next QQ lines contains L,RL,R such that 1≤L≤R≤N1\le L\le R\le N and R−L+1<NR-L+1<N.

Output

Print QQ GCD values, one per query.

Subtasks

  • Subtask 1 (20 points): 2≤N≤302 \le N \le 30; 1≤Q≤N1 \le Q \le N; 1≤Ai≤1031 \le A_i \le 10^3.
  • Subtask 2 (30 points): 2≤N≤50002 \le N \le 5000; 1≤Q≤N1 \le Q \le N; 1≤Ai≤1051 \le A_i \le 10^5.
  • Subtask 3 (50 points): 2≤N≤1052 \le N \le 10^5; 1≤Q≤N1 \le Q \le N; 1≤Ai≤1051 \le A_i \le 10^5.

Examples

Input

3 3
2 6 9
1 1
2 2
2 3

Output

3
1
2

Explanation

Removing index 11 leaves [6,9][6,9], with GCD 33. Removing index 22 leaves [2,9][2,9], with GCD 11. Removing [2,3][2,3] leaves the single number 22, so the GCD is 22.