#K60702. Identify Branch with Lowest Taxable Income

    ID: 31145 Type: Default 1000ms 256MiB

Identify Branch with Lowest Taxable Income

Identify Branch with Lowest Taxable Income

You are given n branches and m sectors. Each branch provides two values per sector: its income and its expenditure.

For each branch, the taxable income for a set of sectors S is calculated as:

$$T = \sum_{j \in S}\Big(\text{income}_j - \text{expenditure}_j\Big)$$

You will be given several queries. Each query consists of an integer x followed by x sector indices. For each query, calculate the taxable income for every branch considering only the specified sectors. The branch having the lowest taxable income should be selected. In case of ties, choose the branch with the smaller branch ID (branches are 1-indexed).

Your task is to output the branch ID for each query that meets these criteria.

inputFormat

The input is read from standard input (stdin) and is structured as follows:

  1. First line contains two integers n and m where n is the number of branches and m is the number of sectors.
  2. The next n lines each contain 2*m integers. For each branch, the first two numbers correspond to income and expenditure of sector 0, the next two numbers for sector 1, and so on.
  3. The next line contains an integer q indicating the number of queries.
  4. Each of the following q lines starts with an integer x (the number of sectors in the query) followed by x space-separated sector indices.

outputFormat

For each query, output a single line with the branch ID (1-indexed) that has the lowest taxable income based on the given sectors.

## sample
3 3
500 100 300 50 200 80
400 200 300 100 200 150
600 200 400 200 100 70
2
2 0 1
3 0 1 2
2

2

</p>