NOI Contest Archive

Archive / 2021 / Selection Test

God's Money

Easy · National Olympiad in Informatics Sri Lanka - Screening Test 2021

Asgard - home to the god of Thunder, has a currency system very different from Earth’s system. Gods being gods, they have infinite types of Asgardian Coins, with their values following the pattern 1939 \(\times\) 10k where 0 \(\leq\) k \(\lt\) \(\infty\) .

So the values of the coins go like 1939, 19390, 193900, and so on until infinity.

Loki, the god of mischief has an infinite supply of Asgardian Coins of all the infinite types. But he’s very greedy about spending his treasures.

Popular for your geniusness, Loki hired you to be his personal finance manager. Your task is simple; given any amount, you have to find the minimum number of coins that Loki needs to settle that exact amount, or state that it’s not possible to settle the required amount even with Loki’s infinite supply of infinite types of Asgardian Coins.

Input Format

Output Format

Print Q lines, the ith line containing the minimum number of coins needed to settle the Aith amount, or -1 if it is impossible to settle the exact amount.

Constraints

For 50% of the testcases:

Sample Input 0

3
1939
1940
193900

Sample Output 0

1
-1
1