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

#GL0000198. Ước chung lớn nhất trong khoảng (Modified GCD)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ước chung lớn nhất trong khoảng (Modified GCD)

Ước chung lớn nhất trong khoảng (Modified GCD)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given fixed positive integers a,ba,b, each query provides [low,high][low,high] and asks for the largest common divisor dd of a,ba,b within that inclusive interval. Print -1 if none exists.

Input

First line: a,ba,b. Second line: qq. Next qq lines: low,highlow,high.

Output

Print one integer per query.

Subtasks

  • Subtask 1 (20 points): 1≤a,b≤100001\le a,b\le 10000; 1≤q≤10001\le q\le 1000; 1≤low≤high≤100001\le low\le high\le 10000.

  • Subtask 2 (30 points): 1≤a,b≤1000000001\le a,b\le 100000000; 1≤q≤50001\le q\le 5000; 1≤low≤high≤1000000001\le low\le high\le 100000000.

  • Subtask 3 (50 points): 1≤a,b≤10000000001\le a,b\le 1000000000; 1≤q≤100001\le q\le 10000; 1≤low≤high≤10000000001\le low\le high\le 1000000000.

Examples

Example 1

Input

9 27
3
1 5
10 11
9 11

Output

3
-1
9

Explanation

The GCD is 99, whose positive divisors are 1,3,91,3,9. The largest in [1,5][1,5] is 33, there is none in [10,11][10,11], and 99 is in [9,11][9,11].

Example 2

Input

12 18
2
1 6
4 5

Output

6
-1

Explanation

The common divisors of 1212 and 1818 are 1,2,3,61,2,3,6; only the first interval contains 66, and the second contains no such divisor.

Example 3

Input

1 1
2
1 1
2 5

Output

1
-1

Explanation

When a=b=1a=b=1, the only common positive divisor is 11.