IOPC1201 - Rubiks cube

The Rubik's cube is perhaps the world's most popular intellectual toy. More than just the joy of solving, there is a lot of mathematics to it too.

Consider a solved Rubik's cube. The six faces of the cube are named FRONT, BACK, UP, DOWN, LEFT and RIGHT respectively. An elementary move of the Rubik's cube is rotating a face by 90 degrees clockwise, 90 degrees anticlockwise or 180 degrees about an axis from the centre of the face to the centre of the cube. Any valid state of the Rubik's cube can be reached by applying these elementary operations one after the other.

An elementary move is denoted in the following fashion. If a given face is rotated by 90 degrees clockwise about the axis passing from the centre of the face to the centre of the cube, the move is denoted by the first letter of the name of the face. If the rotation is anticlockwise by 90 degrees, the letter is followed by an apostrophe ('). If the rotation is by 180 degrees, the letter is followed by a 2.

For example, a 90 degree clockwise rotation of the right face is denoted by R, a 90 degree anticlockwise rotation of the back face by B' and a 180 degree rotation of the top face by U2. The elementary moves are explained with the help of animations in the "Face turns" section of this page. The elementary moves can be combined to get a compound move of the cube. For example, URF2 denotes rotating the top face by 90 degrees clockwise, then the right face by 90 degrees clockwise followed by the front face by 180 degrees.

In this problem you will be given a string describing a sequence of elementary moves on a solved Rubik's cube. Your task is to find out the number of times the sequence should be applied repeatedly to the cube to get back the original cube.

Input

The first line of input gives T, the number of test cases (1≤T≤1000). Following this are T lines, each containing a string denoting a sequence of moves on the Rubik's cube. The string will contain only the characters U, D, L, R, F, B, ' and 2, and will be at most 1000 characters long. It is assured to be a valid sequence of elementary moves.

Output

For each test case, output the minimum number of times (≥1) the move sequence must be applied to a solved Rubik's cube to get back the solved cube again.

Example

Input:
4
R
RR2
RU
R2R'R'

Output:
4
4
105
1

Added by:Raziman T V
Date:2012-01-17
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:http://www.codechef.com/IOPC2012/

hide comments
2014-01-15 07:45:52 miodziu
The page linked in problem description ( http://www.nascarjon.us/notation.html ) doesn't exists.
2014-01-13 12:37:01 Alexandre Henrique Afonso Campos
This was the problem that I enjoyed the most while solving it.

But I was able to solve it after using pg 2 of
http://www.math.ubc.ca/~cass/courses/m308/projects/rtran/rtran.pdf

Before, I've coded it wrong and learned something new.
2012-11-26 15:55:25 Joseph Cozza
This link has some good information, especially pages 5-7.

http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=8&cad=rja&ved=0CGkQFjAH&url=http%3A%2F%2Fweb.mit.edu%2Fsp.268%2Fwww%2Frubik.pdf&ei=wI-zUISSCciKiAKCloCwCg&usg=AFQjCNEgLin-Tnl7cQMbTuNa1JuYLGYMIw&sig2=6E_WZY4Lwh9cS6vTArlFBA
2012-01-17 18:48:52 Raziman T V
Try to work that out, Santiago
2012-01-17 17:06:24 Santiago Palacio
Does de answer always fit in a signed (or unsigned) 64-bit integer? even more, is it always possible to go back to the original position?

Last edit: 2012-01-17 19:24:10
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.