This page is still under construction.

Parts of this page are still being built. What you see may change.

Concentration Cards

Time limit1sMemory limit128 MB

Summary
Given N cards of size W by H that can each be rotated, tile a filled rectangle with them and find the smallest possible perimeter.
Level

Hard8 of 10

Topics
Math, Geometry, Brute force, Implementation
Solved
No attempts yet

Problem

Stan has a deck of NN Concentration Cards. He wants to lay the cards edge-to-edge to form a single, completely filled rectangle whose perimeter is as small as possible. Each card is a rectangle measuring WW mm by HH mm.

Each card may be rotated by 90∘90^\circ, and the cards may be arranged in any way (not necessarily a simple grid) as long as they cover the rectangle exactly, with no gaps and no overlaps.

Figure 1: Concentration Cards

Input

The first line of input contains CC, the number of test cases. Each of the following lines contains NN, WW, and HH, each a positive integer not exceeding 10001000.

Output

For each test case, print on its own line the minimal possible perimeter of the rectangle.

Examples3

  1. Example 1

    Input
    3
    3 300 400
    4 400 300
    7 300 400
    
    Expected output
    2600
    2800
    3800
    
  2. Example 2

    Input
    1
    1 1 1
    
    Expected output
    4
    
  3. Example 3

    Input
    1
    6 5 5
    
    Expected output
    50