Johnny and the JetPack ---------------------- Johnny is a lucky astronaut, he is on a mission to Mars! There, he took a Mars Rover to collect some Martian soil samples. He has done his job, but, unfortunately, his rover broke down. Johnny called his fellow astronauts for help, but they are busy doing other important things and they told him to come back to the base on foot. As we mentioned, Johnny is a lucky guy. He found a JetPack in the trunk of the rover. It's a kind of a backpack with jet engines that allows him to make huge jumps. Johnny, being pretty lazy, decided that he is going to use the JetPack to jump back to the Base instead of walking there. The only problem is that B. N. A. S. A. (Brand New Aeronautics and Space Administration) is cutting operating costs and it charges the astronauts for any excess resources (like JetPack fuel) they use during their mission. (And Johnny is frugal, in addition to being lucky and lazy.) During each jump that he makes, he is going to burn an amount of fuel equal to the square of the distance he flies plus some extra fuel to make a safe landing. For example, if it takes 3 units of fuel to land and Johnny jumps 5 units of length he will use 5*5 + 3 = 28 units of fuel. To make things more complicated, there are some craters on the astronaut's way and he cannot land in them, because that would be too dangerous. He has sent you descriptions of a couple of paths that he can take. You have to help Johnny optimize his jumps to the base. Input ----- In the first line of the input, there will be a single integer N, equal to the number of test cases that will follow. Each test case will be described by two lines. In the first line, there will be a single integer F (<100), equal to the amount of fuel that the JetPack uses for a safe landing. The second line will consist of a string of characters (no more than 500) that describe the terrain Johnny will be jumping on. Each character corresponds to piece of terrain of unit length. The first character will be 'J' and it denotes Johnny's initial position. Each subsequent character will be '-' (minus) for flat land, or 'v' for a crater. The last character in the line will be 'B' for the base that Johnny wants to reach. A sample test case might be: 1 J--v-B The first line tells us that it takes additional 1 unit of fuel to land safely in each jump. The second line states that there are two units of flat land ahead of Johnny, then there is a crater, one unit of flat land and, finally, the base. Remember that he cannot land in the crater. Johnny could jump straight to the base, which would cost him 5*5 + 1 = 26 units of fuel. However, he can make a two unit jump and land before the crater (costing 2*2 + 1 = 5 units), jump over the crater (again, 5 units) and then another jump to the base (for 1*1 + 1 = 2 units), thus using a total of 12 units of fuel. Output ------ For each test case, output a single line with the minimal cost of getting from Johnny's initial position to the base. Sample Input ------------ 3 0 J----B 1 J---v----vv-B 2 Jvvvv-v-vvvvB Sample output ------------- 5 29 60