#Lutece0221. Counting Problem

Counting Problem

Migrated from Lutece 221 Counting Problem

All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.

Description

Snoworld just entered middle school. His math teacher enjoyed asking him strange questions. One of the questions is that how many numbers are there from 11 to KK that doesn’t contain any MM. For example, if KK equals 55 and MM equals 33, the answer would be 44 and if KK equals 1111 and MM equals 11, the answer would be 88. Snoworld solved the question rather easily. So teacher complicated the question by asking it in an NN-base system and required the answer to presented in an nn-base system, so the KK and MM is NN-base number. For example, if KK equals 2222 and MM equals 11 and NN equals 33, all the numbers are 1,2,10,11,12,20,21,221, 2, 10, 11, 12, 20, 21, 22 and the answer is 1010.

Input

The first line of the input is an integer CC representing the number of cases, while is no more than 3030.

The following CC lines each contain three integers K,MK, M and NN.

KK has no more than 99 digits. 0<M<10,1<N10,M<N0 < M < 10, 1 < N \leq 10, M < N.

Output

For each test case print one line with the answer presented in an NN-base system.

Samples

3
11 1 10
22 1 3
36 5 9
8
10
32

Resources

第四届北京邮电大学程序设计竞赛网络预赛