High Score

Given counts of Cog, Tablet, Compass, and Wildcard tokens, assign each Wildcard to one type to maximize a^2+b^2+c^2+7*min(a,b,c).

Medium5GreedyMathBrute forceNo attempts yetTime limit2sMemory limit512 MB

Problem

Mårten and Simon enjoy the board game Seven Wonders and have just finished a match. It is time to tally the scores.

One way to score in Seven Wonders is Science. During the game, each player collects Science tokens of three types: Cog, Tablet, and Compass. A player with aa Cogs, bb Tablets, and cc Compasses scores a2+b2+c2+7×min(a,b,c)a^2 + b^2 + c^2 + 7 \times \min(a, b, c) points.

Wildcard Science tokens complicate the count. Each Wildcard Science token a player holds may be counted as any one of the three ordinary types. For instance, a player with 2 Cogs, 1 Tablet, 2 Compasses, and 1 Wildcard Science token can end up with (3,1,2)(3, 1, 2), (2,2,2)(2, 2, 2), or (2,1,3)(2, 1, 3) Cogs, Tablets, and Compasses. The possible scores are 32+12+22+7×1=213^2 + 1^2 + 2^2 + 7 \times 1 = 21, 22+22+22+7×2=262^2 + 2^2 + 2^2 + 7 \times 2 = 26, and 22+12+32+7×1=212^2 + 1^2 + 3^2 + 7 \times 1 = 21, so the maximum score for this player is 26.

Given the number of tokens each player holds, compute the maximum score each player can reach by assigning the Wildcard Science tokens optimally.

Input

The first line contains an integer nn (3n73 \le n \le 7), the number of players in the game.

Each of the next nn lines contains four integers aa, bb, cc, dd (0a,b,c,d1090 \le a, b, c, d \le 10^9): the number of Cog, Tablet, Compass, and Wildcard Science tokens of one player.

Output

For each player, in input order, print the maximum score that player can get, one per line.