CF2179A.Blackslex and Password

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Blackslex and Password

Blackslex is designing a log-in system for Gean Dev and discovered that most users use weak passwords.

To resolve this issue, he posed the following conditions, dependent on two variables kk and xx, for all passwords. Each password is a string ss of length nn satisfying these properties.

  • ss uses only the first kk lowercase letters of the English alphabet.
  • For every pair of indices 1i<jn1 \le i \lt j \le n such that (ji)(j-i) is divisible by xx, the letters sis_i and sjs_j are different.

Find the smallest integer nn such that no valid string of length nn exists.

Input

The first line contains a single integer tt (1t5001 \le t \le 500) — the number of test cases.

The first and only line of each test case contains two integers kk and xx (1k261 \le k \le 26, 1x151 \le x \le 15).

Output

For each test case, output the minimal nn.

Note

For the first test case, there are no valid strings of length n=3n=3. For n=2n=2, one such valid example is ab. Note that the only pair (i,j)(i, j) that (ji)(j-i) is divisible by x=1x=1 and 1i<jn1 \le i \lt j \le n for n=2n=2 is (1,2)(1, 2).

For the second test case, there are no valid strings of length n=7n=7. For n=6n=6, one such valid example is aabccb. Note that all pairs (i,j)(i, j) that (ji)(j-i) is divisible by x=2x=2 and 1i<jn1 \le i \lt j \le n for n=6n=6 include (1,3)(1, 3), (1,5)(1, 5), (2,4)(2, 4), (2,6)(2, 6), (3,5)(3, 5), and (4,6)(4, 6).

For the third test case, there are no valid strings of length n=6n=6. For n=5n=5, one such valid example is aaaaa. Note that there are no pairs (i,j)(i, j) that (ji)(j-i) is divisible by x=5x=5 and 1i<jn1 \le i \lt j \le n for n=5n=5.

Samples

3
2 1
3 2
1 5
3
7
6

在线编程 IDE

建议全屏模式获得最佳体验