403289: GYM101102 I Simple Robot

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

I. Simple Robottime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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?

Input

The 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.

Output

For each test case, print the minimum number of instructions which will be skipped, on a single line.

ExampleInput
2
2 2 >^<v>^^<v
3 4 >^<^vv<>v>>v
Output
1
2

加入题单

算法标签: