Tornado operation homework
Time limit1sMemory limit128 MB
Choose the fewest nondecreasing addition and exponent steps so the iterated value plus C is a multiple of 10 to the P.
- Level
Medium7 of 10
- Topics
- BFS, Graph, Number theory
- Solved
- No attempts yet
Problem
Time for your math homework. A Tornado operation is defined as follows.
is a given constant and is the set of positive integers. and are positive integers, chosen so that and hold for every positive . They also satisfy and for every positive .
For example, if , , and , then
You are given , , , , and two positive integers and . Your homework is to find the smallest for which and can be chosen so that is divisible by . Here is a non-negative integer, and means that no operation is applied at all, so the value is .
Input
The input holds one or more test cases. The first line contains a single integer , the number of test cases (). Each of the next lines holds one test case.
Each of those lines contains 7 integers , , , , , and in this order, separated by single spaces (, , ).
Output
For each test case, print on a single line the smallest such that is divisible by . If no such exists, print -1 instead.