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

#GL0000296. Bài toán đếm khác nhau (Yet Another Counting Problem)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Bài toán đếm khác nhau (Yet Another Counting Problem)

Yet Another Counting Problem

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two positive integers a,ba,b and qq independent queries. Each query specifies an inclusive integer interval [l,r][l,r].

For an integer xx, consider the two nested remainders taken in opposite orders:

p=(x mod a) mod b,t=(x mod b) mod a.p=(x\bmod a)\bmod b,\qquad t=(x\bmod b)\bmod a.

These expressions may differ. For each query, count the integers xx satisfying l≤x≤rl\le x\le r and p≠tp\ne t. The parameters aa and bb stay unchanged for every query.

Input

The first line contains a,b,qa,b,q. Each of the following qq lines contains l,rl,r, the inclusive endpoints of one query.

Output

Print one line of exactly qq space-separated integers in query order. The ii-th number is the required count for query ii.

Subtasks

  • Subtask 1 (20 points): 1≤a≤201\le a\le 20; 1≤b≤201\le b\le 20; 1≤q≤201\le q\le 20; 1≤li≤ri≤100001\le l_i\le r_i\le 10000 for each 1≤i≤q1\le i\le q.
  • Subtask 2 (30 points): 1≤a≤2001\le a\le 200; 1≤b≤2001\le b\le 200; 1≤q≤1001\le q\le 100; 1≤li≤ri≤10000000001\le l_i\le r_i\le 1000000000 for each 1≤i≤q1\le i\le q.
  • Subtask 3 (50 points): 1≤a≤2001\le a\le 200; 1≤b≤2001\le b\le 200; 1≤q≤5001\le q\le 500; 1≤li≤ri≤10000000000000000001\le l_i\le r_i\le 1000000000000000000 for each 1≤i≤q1\le i\le q.

Examples

Examples 1

Input

4 6 5
1 1
1 3
1 5
1 7
1 9

Output

0 0 0 2 4

Explanation

For a=4,b=6a=4,b=6, the integers 6,7,8,96,7,8,9 satisfy the inequality, whereas 11 through 55 do not. The five query answers are 0,0,0,2,40,0,0,2,4.

Examples 2

Input

7 10 2
7 8
100 200

Output

0 91

Explanation

The interval [7,8][7,8] contributes zero. The period is lcm⁡(7,10)=70\operatorname{lcm}(7,10)=70, and positions with period remainder at least 1010 qualify. Counting within [100,200][100,200] gives 9191.