Boxes

Interview

Time limit1sMemory limit128 MB

Summary
For each item, find the standard box with the smallest volume that can contain it after 90-degree rotations, or report that none fits.
Level

Medium4 of 10

Topics
Sorting, Implementation
Solved
No attempts yet

Problem

Nowadays almost any item can be bought and sold on the internet. The challenge is shipping. Before an item can be sent, it must be carefully packed in a cardboard box to protect it.

Items come in all shapes and sizes, so finding a box of just the right size can be difficult. If the box is too small, the item will not fit. If the box is unnecessarily large, shipping costs more, and the item is more likely to move around inside the box and get damaged.

Cardboard box manufacturers offer a fixed set of standard box sizes. Your task is to find the standard box with the smallest volume into which an item will fit.

Each box is a rectangular prism with a given length, width, and height. Each item is likewise a rectangular prism with a given length, width, and height. Before packing, an item may be rotated by any multiple of 90 degrees in any direction, but once packed its faces must be parallel to the faces of the box. An item fits into a box as long as each of its dimensions is less than or equal to the corresponding dimension of the box.

Input

The first line contains a single integer nn (0<n<10000 < n < 1000), the number of available box sizes. Each of the next nn lines contains three integers giving the length, width, and height of a box. The following line contains a single integer mm (0<m<10000 < m < 1000), the number of items to be packaged. Each of the next mm lines contains three integers giving the length, width, and height of an item. All dimensions are in millimetres and range from 1 mm1\,\text{mm} to 2000 mm2000\,\text{mm}.

Output

Output mm lines, one for each item. For each item, output a single integer: the volume (in mm3\text{mm}^3) of the smallest box into which the item fits. The same box size may be reused for any number of items. If an item does not fit in any box, output the line Item does not fit.

Examples1

  1. Example 1

    Input
    3
    1 2 3
    2 3 4
    3 4 5
    5
    1 1 1
    2 2 2
    4 3 2
    4 3 3
    4 4 4
    
    Expected output
    6
    24
    24
    60
    Item does not fit.