Олимпиадный тренинг

Задача 39361. Pair with the highest amount


The program input receives pairs of numbers: a natural number N and the base of the number system r, in which the number N is written. The sign of the end of the input is the pair 0 0. Print a pair of numbers with the maximum sum on the screen. Display numbers in decimal notation. In this problem, by a pair we mean two numbers located side by side.

Input
The input is an unknown number of lines. Each line, except the last one, contains 2 numbers: N (1 <= N <= 107) and r (2 <= N <= 9). The last line contains a pair of 0 0 (a sign of the end of input).

Imprint
Print the answer  a pair of numbers with the maximum sum. If there are several such pairs, print the first one.
 
Examples
# Input Output Explanation
1 3 5
21 4
1 3
18
23
0 0
3 9 In the source data we have the following numbers
35, 214, 13, 18, 23< br /> The pairs are as follows:
(35, 214), (214, 13), (13 , 18), (18, 23)
The pair with the highest sum (35, 214). In decimal notation, these are numbers (3, 9)
Answer: 3 9