Puzzle: X-Sums Sudoku
时间限制:C/C++/Rust/Pascal 4秒,其他语言8秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

An sudoku puzzle is a grid consisting of regions, and each region contains cells. Hence an sudoku puzzle contains cells. Every integer from 1 to nm occurs exactly once in each row, each column, and each region of an sudoku puzzle.

Listing the integers in a row or a column starting from some direction as a sequence of length nm, X is the first integer of the sequence, and X-sum is the sum of the first X integers of the sequence.



The above figure is a sudoku puzzle with X-sums. The 7-th row listed from right to left is and the first integer X is 3, so the X-sum of the 7-th row from the direction right is .

Given two positive integers n and m, a direction d, and an index x, you need to find the X-sum of the x-th row or x-th column from the direction d in the lexicographically smallest  sudoku.

Denoting as the i-th row and the j-th column of a sudoku puzzle a, a sudoku puzzle a is lexicographically smaller than a sudoku puzzle b of the same size if there exists i and j satisfying that , that for all , and that for all and . You can find that the above is the lexicographically smallest sudoku puzzle.

输入描述:

There are multiple test cases. The first line of input contains an integer T(), the number of test cases.

For each test case:

The only line contains two integers n and m (, ), a string d, and an integer x (). is the size of the sudoku puzzle. d is the direction of X sum, and it is one of "left", "right", "top", and "bottom". x is the index of a row or a column.

输出描述:

For each test case:

Output an integer -- the X-sum of the x-th row or x-th column from the direction d in the lexicographically smallest  sudoku.

Note that the answer may exceed . Consider using __int128_t in C++, BigInteger in Java or Kotlin, or int in Python.
示例1

输入

复制
4
2 1 top 1
2 1 bottom 2
2 1 left 3
2 1 right 4

输出

复制
1
34
27
3
示例2

输入

复制
4
11 19 top 1053766555
12 26 top 230781535210
14 10 right 8344647
7 30 right 70120568170

输出

复制
565741033271081135
31719572400444316026492
112693473538824
477453505821905419941