Woburn Challenge 2002
The cows sense victory. With the monkeys "pinned" squarely in the middle of Scarberia, attending to their tender behinds, the cows have decided to go in for the kill. Before they do so, however, they must gather some intelligence. To this end, the leader of the cows, Bo Vine, has designed a robot. He calls it… (Insert drum roll here)… COW-BOT! As with every computer program, ahem, Cow-Bot has to be tested rigorously and thoroughly to ensure that there are no flaws in his implementation.
Bo Vine has fed into the robot a list of instructions that it must execute. Cow-Bot will be presented with an initial number and be given the number of iterations it must perform. He begins by considering instructions at the beginning of the list, and executes the first of these that apply. The result of the action is the number used in the following iteration, which again begins at the top of the list.
Condition | Action |
---|---|
Number is prime | Multiply by ; e.g.: |
Number is a perfect square | Add the reverse of the number; e.g.: |
Number is a palindrome | Add a at the end; e.g.: |
Number starts with a | Add a at the front; e.g.: |
Number has a anywhere in it | Remove the last digit; e.g.: |
Number is divisible by | Remove the first digit; e.g.: |
Number has an even number of digits | Place a in the middle; e.g.: |
Number has an odd number of digits | Add ; e.g.: |
Input Specification
The first line of input contains a single integer ,
indicating the number of test cases.
Each test case consists of one line of numbers, and , where
, is the starting number and
, is the number of iterations to be performed.
Output Specification
For each case, output the number that is the final result of all computations.
Sample Input
2
5 3
545 5
Sample Output
785
5144
Note: we guarantee that any result, intermediate or final, that is produced by the Cow-Bot is not greater than .
Comments