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

#FT0000056. Xóa các bội số (Removal of Multiples)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Xóa các bội số (Removal of Multiples)

Removal of Multiples

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Initially, SS is the infinite set of all positive integers 1,2,3,…1,2,3,\ldots. Each query gives Ai,BiA_i,B_i. First remove permanently all positive multiples of AiA_i from SS. Then list the surviving elements in increasing order and print the one with one-based rank BiB_i.

Deletions accumulate and are never undone. Repeatedly deleting multiples of the same divisor makes no additional changes. The requested survivor is always guaranteed to exist.

Input

  • Line 1: integer QQ, the query count.
  • Next QQ lines: integers Ai,BiA_i,B_i specifying the divisor to remove and the survivor rank to report.

Output

Print QQ lines; line ii is the BiB_i-th smallest surviving positive integer after query ii.

Subtasks

Subtask 1 (20 points):

  • 1≤Q≤201\le Q\le 20; 2≤Ai≤1092\le A_i\le10^9 and 1≤Bi≤201\le B_i\le 20 for all 1≤i≤Q1\le i\le Q.

Subtask 2 (30 points):

  • 1≤Q≤20001\le Q\le 2000; 2≤Ai≤1092\le A_i\le10^9 and 1≤Bi≤20001\le B_i\le 2000 for all 1≤i≤Q1\le i\le Q.

Subtask 3 (50 points):

  • 1≤Q≤1000001\le Q\le 100000; 2≤Ai≤1092\le A_i\le10^9 and 1≤Bi≤1000001\le B_i\le 100000 for all 1≤i≤Q1\le i\le Q.

Examples

Examples 1

Input

5
5 10
6 1
6 10
9 10
123456789 111

Output

12
1
14
16
178

Explanation

After deleting multiples of 5, the first ten survivors are 1,2,3,4,6,7,8,9,11,121,2,3,4,6,7,8,9,11,12, so print 12. Deleting multiples of 6 still leaves 1 as the smallest. Repeating the same deletion changes nothing; rank 10 is then 14. Removing multiples of 9 changes rank 10 to 16. Removing multiples of 123456789 cannot affect the earliest 111 surviving integers, whose 111th value is 178.

Examples 2

Input

3
2 5
3 5
2 1

Output

9
13
1

Explanation

Removing even numbers leaves 1,3,5,7,9,…1,3,5,7,9,\ldots, giving 9 at rank five. Next remove multiples of 3, leaving 1,5,7,11,13,…1,5,7,11,13,\ldots, giving 13 at rank five. Deleting even numbers again changes nothing; the smallest survivor is still 1.