Permutation Primes

Permutations of a sequence of decimal digits have an interesting property. Any two permutations of a se- quence of digits have a difference, which is divisible by 9. Quite interesting, isn’t it? For example: |458967 − 456879| = 2088 = 9 ∗ 232 We won’t ask for the proof today (as it is very easy) but we will focus towards a different aspect of this property. There are some numbers whose differ- ence with one (or more) of its permutation is of the form 9p, where p is a prime less than 1111111. These numbers are called permutation primes. For example 92-29=63=9*7, where 7 is a prime. So 92 is a per- mutation prime. Now you have to write a program that finds out how many permutation primes are there within a specified range. Input First line of input contains an integer T (0 < T < 51) denoting the number of test cases to follow. Then follows T lines each of which contains two positive integers p and q. Both of them are less than 99999999, without any leading zero(s) and |p − q| ≤ 1000. Output There will be one line of output for each test case. At first print ‘Case i: ’ (without the quotes) where i is an integer denoting the i-th test case starting from one. Then the line will contain an integer N that denotes the number of permutation primes between p and q (inclusive). Sample Input 2 1 10 1 20 Sample Output Case 1: 0 Case 2: 5