#K5466. Smallest Prime in a Range

    ID: 29803 Type: Default 1000ms 256MiB

Smallest Prime in a Range

Smallest Prime in a Range

You are given a range [a, b] and your task is to find the smallest prime number within this inclusive range. If there is no prime number in the range, output -1.

A prime number is defined as a natural number greater than 1 that has no positive divisors other than 1 and itself. The condition can be mathematically expressed using the trial division method. For example, a number n is prime if and only if it is not divisible by any integer in the range \( 2 \leq i \leq \sqrt{n} \).

Input:
The first line of input contains an integer T, representing the number of test cases. Each of the following T lines contains two space-separated integers, a and b, representing the lower and upper bounds of the range.

Output:
For each test case, output the smallest prime number in the range [a, b]. If no prime exists within the range, output -1.

Note: The solution must read from standard input and write to standard output.

inputFormat

The input starts with an integer T denoting the number of test cases. Each of the next T lines contains two integers a and b, where a is the beginning and b is the end of the range (inclusive).

outputFormat

For each test case, output a single integer — the smallest prime number in the range [a, b]. If there is no prime in the range, output -1.

## sample
5
10 20
24 28
2 2
8 10
29 33
11

-1 2 -1 29

</p>