OMWG - One more weird game


Leha is playing a very interesting game. The game will be played on a rectangular grid consisting of N rows and M columns. Initially all the cells of the grid are uncolored.

Leha's initial score is zero. At each turn, he chooses some cell that is yet not colored, and colors that cell. The score obtained in this step will be number of neighboring colored cells of the cell that Leha colored in this step. Two cells are neighbors of each other if they share a side between them. The game will end when all the cells are colored. Finally, total score obtained at the end of the game will sum of score obtained in each turn.

Leha wants to know what maximum score he can get? Can you please help him in finding this out?

Input

The first line contains a single integer T denoting the number of test cases. T test cases follow.

Each of the following T lines contains two space-separated integers N, M denoting the dimensions of the grid.

Output

For each test case, output a single line containing an integer corresponding to the maximal possible score Leha can obtain.

Constraints

1 ≤ T ≤ 100

0 ≤ N, M ≤ 1 000

Example

Input:
1
2 2

Output:
4

Explanation

Leha can obtain total score 4 in the following way.

  1. In the first step, he colors down left cell, all the neighboring cells of this cell are uncolored. So, it adds 0 to total score.
  2. In second step, he can color upper right cell, it also adds total 0 to the score.
  3. In third step, he can color top left cell. There are two neighboring cell of this cell, both of which are colored. So, this add 2 to the score.
  4. In the last step, he can choose the remaining down right cell. There are two neighboring cell of this cell, both of which are colored. So, this also add 2 to the score.

Leha can't obtain a score more than 4. Hence 4 is the answer.


hide comments
anonymous: 2024-07-07 09:16:59

Problem statement is inconsistent with test data. There are test cases with N or M equal to 0, even though problem statement claims that "1 ≤ N, M ≤ 1 000".
[Simes]: agreed. I've updated the range in the problem statement.

Last edit: 2024-07-08 07:53:15
ratnasingh: 2020-03-08 12:51:59

graph tag is misleading

murdangorguti: 2019-10-31 16:23:54

ll low=n*(m-1);
ll high=m*(n-1);

cout<<(low+high)<<endl;

darthcoder3200: 2019-07-21 15:18:32

flood fill algo

anirudnits: 2018-10-09 18:48:46

the graph approach passes :)

aniket000: 2018-01-31 14:52:08

nice! , no idea how to solve using graphs, but solved it in O(1) xd

shiv2111: 2017-12-21 11:04:13

graph contains vertices and "EDGES" :).

abhilc: 2017-05-12 17:30:48

O(1) is giving me TLE. What kind of sorcery is this? :o

iharsh234: 2016-08-21 13:09:49

tukka got AC. haha

akshayvenkat: 2016-07-14 16:48:49

there are only two TLE's for this problem ever and both are mine :p


Added by:Beer hu !!
Date:2016-07-01
Time limit:0.5s-1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU JS-MONKEY
Resource:Codechef