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

#GL0000238. Phần tử thứ k nguyên tố cùng nhau với p (List Of Integers)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Phần tử thứ k nguyên tố cùng nhau với p (List Of Integers)

Phần tử thứ k nguyên tố cùng nhau với p (List Of Integers)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

For each query (x,p,k)(x,p,k), consider the increasing sequence of integers y>xy>x satisfying gcd⁡(y,p)=1\gcd(y,p)=1. Output its kk-th element.

Input

  • First line: number of queries qq.
  • Each of the next qq lines contains x,p,kx,p,k.

Output

Print one integer answer per query.

Subtasks

  • Subtask 1 (20 points): 1≤q≤10, 1≤x,p,k≤1001\le q\le10,\ 1\le x,p,k\le100.
  • Subtask 2 (30 points): 1≤q≤1000, 1≤x,p,k≤1041\le q\le1000,\ 1\le x,p,k\le10^4.
  • Subtask 3 (50 points): 1≤q≤30000, 1≤x,p,k≤1061\le q\le30000,\ 1\le x,p,k\le10^6.

Examples

Example 1

Input

3
7 22 1
7 22 2
7 22 3

Output

9
13
15

Explanation

The first three integers greater than 7 and coprime with 22 are 9, 13, and 15.

Example 2

Input

1
1 1 5

Output

6

Explanation

Every integer is coprime with 1, so the fifth integer after 1 is 6.