#K41882. Maximum Colors on Grid

    ID: 26964 Type: Default 1000ms 256MiB

Maximum Colors on Grid

Maximum Colors on Grid

You are given a grid with r rows and c columns. Your task is to determine the maximum number of different colors that can be used to color the cells of the grid such that no two adjacent cells (sharing a common side) have the same color.

For this problem, the rules are as follows:

  • If r = 1 or c = 1 (i.e. the grid is one-dimensional), you can only use 1 distinct color.
  • Otherwise, the answer is the minimum of 9 and r × c. In mathematical terms, if r > 1 and c > 1, then the maximum number of colors is given by \[ \min(9, r \times c). \]</p>

    The input will contain multiple test cases. For each test case, you will be given two integers representing the number of rows and columns. Your program should output the answer for each test case on a separate line.

    inputFormat

    The first line of input contains an integer T representing the number of test cases. Each of the next T lines contains two space-separated integers r and c, representing the number of rows and columns of the grid.

    outputFormat

    For each test case, output a single integer on a new line — the maximum number of different colors that can be used under the given conditions.

    ## sample
    1
    1 5
    
    1