403289: GYM101102 I Simple Robot
Description
SaMer is building a simple robot that can move in the four directions: up (^), down (v), left (<), and right (>). The robot receives the commands to move as a string and executes them sequentially. The robot skips the commands that make it go outside the table.
SaMer has an R×C table and a string of instructions. He wants to place the robot on some cell of the table without rotating it, such that the robot will skip the minimum number of instructions. Can you help him find the number of instructions that will be skipped by the robot if it was placed optimally?
InputThe first line of input contains a single integer T, the number of test cases.
Each test case contains two space-separated integers R and C (1 ≤ R, C ≤ 105), the number of rows and the number of columns in the table, followed by a non-empty string of no more than 200000 characters representing the initial instructions. The instructions are executed in the given order from left to right.
OutputFor each test case, print the minimum number of instructions which will be skipped, on a single line.
ExampleInput2Output
2 2 >^<v>^^<v
3 4 >^<^vv<>v>>v
1
2