SP392 SPIN - Spin

Description

![tex2html_wrap62](https://cdn.luogu.com.cn/upload/vjudge_pic/SP392/bb4f60a1c9505eb2512de18dff0c428fe93d6b80.png) ![tex2html_wrap64](https://cdn.luogu.com.cn/upload/vjudge_pic/SP392/276ff2add3964e4f49b03a2156843b56da29f9d6.png)A disk can be rotated between horizontal and vertical only if it is positioned over the indentation marked `0' _and_ the disk on its right is vertical . The right-most disk can always rotate if it is in position `0' since it has no disk on its right. The aim is to free the slide by moving it so its left edge aligns with the `Win' mark: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/SP392/6e5ad06ca0a5e438bc25a31a0d1bedb60fef48c4.png) Your task is to write a program which will take several part-solved puzzles and compute the number of steps needed to move the slide to position `Win' for each puzzle.

Input Format

There will be several puzzles in the input file. The first line of the file will contain an integer _n_ specifying the number of puzzles. There will then be _n_ lines, each of the form: _length orientations position_ where _length_(length < 30) is an integer indicating the number of disks on the slide, _orientations_ is a string of _length_ characters from the set {h,v} giving the orientation of each disk from left to right (h stands for horizontal, and v for vertical), and _position_ is an integer from 0 to _length_ specifying the numbered mark which aligns with the left edge of the slide.

Output Format

For each puzzle, your program should output one integer on a line which counts the minimum number of steps needed to win the puzzle. A step is either a movement of the slide, one unit left or right, or the rotation of a disk.