DSLR
InterviewTime limit6sMemory limit256 MB
Find the shortest DSLR command string that turns register value A into target B, breaking ties in alphabetical order.
Problem
There is a simple calculator that uses four commands: , , , and . The calculator has a single register that stores a decimal number with . Each command transforms the value currently in the register. Let the four digits of be , so that .
- D: doubles . If the result is greater than , it is reduced modulo ; that is, the register receives .
- S: stores . If is , it stores instead.
- L: rotates the four digits of one place to the left. Afterwards the digits, from the left, are .
- R: rotates the four digits of one place to the right. Afterwards the digits, from the left, are .
and always rotate over four decimal digits. For example, if , then gives and gives .
Given two distinct integers and (), write a program that finds the shortest command string that turns into . For example, if and , two commands suffice in either of these ways:
- apply twice:
- apply twice:
Here the two shortest command strings are LL and RR, and you must output LL, which comes first in alphabetical order.
Be careful when a digit is . For example, applying to gives , so the result is ; applying gives , so the result is .
Input
The first line contains the number of test cases . Each of the next lines contains two integers and separated by a space, where is the register's initial value and is the target value. Both satisfy and .
Output
For each test case, print on its own line the shortest command string that turns into . If several command strings share the minimum length, print the lexicographically smallest one, comparing command letters in alphabetical order .