MofK wants to upgrade his old TV by taking it to upgrade shops throughout the city he lives in. The city is laid out in a grid, with ~n~ horizontal roads and ~m~ vertical roads. When viewed on a map, the horizontal roads are numbered from ~1~ to ~n~ from top to bottom, and the vertical roads are numbered from ~1~ to ~m~ from left to right. The intersection of the ~r~-th horizontal road and the ~c~-th vertical road is denoted as ~(r, c)~.
MofK starts at position ~(1, 1)~. He will move along the roads with the goal of reaching the Golden Electronics store at position ~(n, m)~. To get to the Golden Electronics as quickly as possible, at each step, from position ~(r, c)~, MofK will move to either ~(r + 1, c)~ or ~(r, c + 1)~. MofK will not move outside the city.
Fortunately, at each intersection, there is an upgrade shop for TVs. When MofK reaches an upgrade shop (including the shop at the starting position and the Golden Electronics store at the destination), he immediately buys a TV upgrade at that shop. Of course, this also means that the TV will change size:
Before starting, the TV has a height of ~h~ and a width of ~w~.
When upgraded at the shop located at intersection ~(i, j)~:
The height of the TV increases by ~a_i~;
The width of the TV increases by ~b_j~.
Where ~a_1, a_2, \ldots, a_n~ and ~b_1, b_2, \ldots, b_m~ are two sequences of positive integers.
MofK wants the diagonal size of his TV to be as large as possible. In other words, among all valid paths from position ~(1, 1)~ to ~(n, m)~, help MofK find the path such that when he reaches the Golden Electronics store, the value of ~h^2 + w^2~ (where ~h~ is the height and ~w~ is the width of the TV) is maximized. Output ~h^2 + w^2~ at the Golden Electronics store after MofK takes that path.
Input
The first line contains an integer ~t~ (~1 \le t \le 100~) — the number of test cases. The description of each test case is as follows.
The first line contains four integers ~n~, ~m~, ~h~, ~w~ (~1 \le n, m \le 1000~, ~0 \le h, w \le 10^6~) — the number of horizontal roads, the number of vertical roads, the initial height, and the initial width of MofK's TV.
The second line contains ~n~ integers ~a_1, a_2, \ldots, a_n~ (~0 \le a_i \le 10^6~) — where ~a_i~ is the height that the TV will increase when upgraded at any shop on the ~i~-th horizontal road.
The third line contains ~m~ integers ~b_1, b_2, \ldots, b_m~ (~0 \le b_j \le 10^6~) — where ~b_j~ is the width that the TV will increase when upgraded at any shop on the ~j~-th vertical road.
It is guaranteed that:
the total ~n~ across all test cases does not exceed ~1000~;
the total ~m~ across all test cases does not exceed ~1000~;
Output
For each test case, output a single integer which is the maximum possible value of ~h^2 + w^2~ that MofK can achieve when moving from position ~(1, 1)~ to ~(n, m)~ along valid paths.
Scoring
Subtask | Points | Constraints |
---|---|---|
1 | ~500~ | The total of ~n~ and ~m~ across all test cases does not exceed ~50~, and ~h, w, a_i, b_i \le 50~ |
2 | ~1500~ | ~h, w, a_i, b_i \le 1000~ |
3 | ~2000~ | No additional constraints |
Total | ~4000~ |
Sample Input 1
4
1 3 1 1
2
1 2 3
2 2 10 20
10 20
30 40
2 3 3 1
4 1
5 9 2
10 6 11 9
7 30 49 49 48 10 45 41 37 12
31 34 7 38 19 39
Sample Output 1
98
19400
845
603200
Notes
In the first test case, MofK has only one way to go:
Initially, MofK is at ~(1, 1)~. The height increases to ~h = 1 + 2 = 3~, and the width increases to ~w = 1 + 1 = 2~.
MofK moves to ~(1, 2)~. The height increases to ~h = 3 + 2 = 5~, and the width increases to ~w = 2 + 2 = 4~.
MofK moves to ~(1, 3)~. The height increases to ~h = 5 + 2 = 7~, and the width increases to ~w = 4 + 3 = 7~.
Finally, ~h^2 + w^2 = 7^2 + 7^2 = 98~.
In the second test case, MofK's optimal path is:
Initially, MofK is at ~(1, 1)~. The height increases to ~h = 10 + 10 = 20~, and the width increases to ~w = 20 + 30 = 50~.
MofK moves to ~(1, 2)~. The height increases to ~h = 20 + 10 = 30~, and the width increases to ~w = 50 + 40 = 90~.
MofK moves to ~(2, 2)~. The height increases to ~h = 30 + 20 = 50~, and the width increases to ~w = 90 + 40 = 130~.
Finally, ~h^2 + w^2 = 50^2 + 130^2 = 19400~.
Comments