Kth Prime Sum
77% Success294 Attempts50 Points5s Time Limit256MB Memory1024 KB Max Code

We are given \(Q\) queries in the form of "\(L \space R \space K\)".
For each query you are asked to print the \(k^{th}\) smallest number in the range \(L \) to \(R\) (both inclusively) whose digit sum is a prime number or report (print \(-1\)) if it doesn't exist.

Input Format

  • First line of input contains an integer \(Q\).
  • Next \(Q\) lines contain three space-separated integers \(L \space R \space K\) denoting the query.

Output Format
For each \(i^{th}\) query, print a single integer denoting the answer to the \(i^{th}\) query.

Constraints
\(1 \le Q \le 500\)

\(1 \le L \le R \le 10^{18}\)
\(1 \le K \le R - L + 1\)

Examples
Input
5
3 7 2
6 6 1
10 100 15
10 1000 115
10 10000 1115
Output
5
-1
43
319
3358
Explanation

For the first query: The numbers in the range are \([3, 4, 5, 6, 7]\) in which \(3, 5\) and \(7\) are having the digit sum a prime and \(2nd\) of them is \(5\) so we will output \(5\).

For the second query: There is only one number in the range \([6]\) in which none is having the digit sum a prime and \(1st\) of them deesn't exist so we will output \(-1\).

For the third query: The numbers in the range are \([10, 12, 13, 14, 15 ... 100]\) in which \([11, 12, 14, 16, 20, 21, 23, 25, 29, 30, 32, 34, 38, 41,43, ... 98]\) are having the digit sum a prime and \(15th\) ot them is \(43\) so we will output \(43\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading Editor...
Results