CF div3 753(E) Robot on the Board 1

The robot is located on a checkered rectangular board of size × ( rows, columns). The rows in the board are numbered from 1 to  from top to bottom, and the columns — from 1 to  from left to right.

The robot is able to move from the current cell to one of the four cells adjacent by side.

The sequence of commands  executed by the robot is given. Each command is denoted by one of the symbols 'L', 'R', 'D' or 'U', and triggers the movement to left, right, down or up, respectively.

The robot can start its movement in any cell. The robot executes the commands starting from the first one, strictly in the order in which they are listed in . If the robot moves beyond the edge of the board, it falls and breaks. A command that causes the robot to break is not considered successfully executed.

The robot's task is to execute as many commands as possible without falling off the board. For example, on board 3×33×3, if the robot starts a sequence of actions ="RRDLUU" ("right", "right", "down", "left", "up", "up") from the central cell, the robot will perform one command, then the next command will force him to cross the edge. If the robot starts moving from the cell (2,1) (second row, first column) then all commands will be executed successfully and the robot will stop at the cell (1,2) (first row, second column).

CF div3 753(E) Robot on the Board 1_第1张图片

The robot starts from cell (2,1) (second row, first column). It moves right, right, down, left, up, and up. In this case it ends in the cell (1,2) (first row, second column).

Determine the cell from which the robot should start its movement in order to execute as many commands as possible.

Input

The first line contains an integer  (1≤≤104) — the number of test cases.

The next 2 lines contain descriptions of the test cases.

In the description of each test case, the first line contains two integers  and  (1≤,≤106) — the height and width of the field that the robot is located on. The second line of the description is a string  consisting solely of characters 'L', 'R', 'D' and 'U' — the sequence of commands the robot executes. The string has a length from 1 to 106 commands.

It is guaranteed that the total length of  over all test cases does not exceed 106106.

Output

Print  lines, each of which contains the answer to the corresponding test case. The answer to the test case are two integers (1≤≤) and  (1≤≤), separated by a space — the coordinates of the cell (row number and column number) from which the robot should start moving to perform as many commands as possible.

If there are several such cells, you may output any of them.

Example

input

Copy

4
1 1
L
1 2
L
3 3
RRDLUU
4 3
LUURRDDLLLUU

output

Copy

1 1
1 2
2 1
3 2

 

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define endl '\n'
#define int long long
typedef pairPII;
mapmp;
constexpr int N=1e5+7;

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
       int n,m;
       cin>>n>>m;
       string s;
       cin>>s;
       int len=s.size();
       int res1=0,res2=0;
       int ans=0,ans1=0,ans2=0;
       for(int i=0;ians1) ans1=ans;
           if(ansans1) ans1=ans;
            if(ans

 

你可能感兴趣的:(算法,c++)