This page is still under construction.

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

Vase Collection

Time limit1sMemory limit128 MB

Summary
Given up to 100 shape-decoration pairs drawn from a 36 by 36 grid, find the largest k for which some k shapes and k decorations form a complete k by k block of owned pairs.
Level

Medium7 of 10

Topics
Brute force, Backtracking, Graph, Bit manipulation
Solved
No attempts yet

Problem

Mr. Cheng collects old Chinese porcelain, and in particular late-15th-century Feng dynasty vases. Vase-making of that era followed very strict artistic rules. There were exactly 36 shapes and 36 decoration patterns, giving 36×36=129636 \times 36 = 1296 distinct styles in all.

The obvious goal for a collector is to own one sample of every one of the 1296 styles. Like most collectors, however, Mr. Cheng can never afford a complete collection, so he concentrates on some shapes and some decorations. Because symmetry between shape and decoration was a central aesthetic principle of the Feng dynasty, Mr. Cheng wants the largest possible balanced sub-collection: for as large a kk as possible, he wants a set of kk shapes and kk decorations such that his collection contains all k×kk \times k combined styles of those shapes and decorations.

Determining this kk for a given collection is not always easy, which means his collection might actually be better than he thinks. Given the vases he owns, help him find the largest such kk.

Input

The first line contains a single positive integer nn, the number of test scenarios that follow.

Each scenario begins with a line containing a single positive integer mm (m≤100m \le 100), the number of vases in the collection. Then follow mm lines, one per vase, each containing two integers sis_i and did_i separated by a single space, where sis_i (1≤si≤361 \le s_i \le 36) is the shape of the ii-th vase and did_i (1≤di≤361 \le d_i \le 36) is its decoration.

Output

For each scenario, output one line containing the maximum kk such that there exist kk shapes and kk decorations for which the collection contains all k×kk \times k combined styles.

Examples4

  1. Example 1

    Input
    2
    5
    11 13
    23 5
    17 36
    11 5
    23 13
    2
    23 15
    15 23
    
    Expected output
    2
    1
    
  2. Example 2

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

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

    Input
    1
    16
    1 1
    1 2
    1 3
    1 4
    2 1
    2 2
    2 3
    2 4
    3 1
    3 2
    3 3
    3 4
    4 1
    4 2
    4 3
    4 4
    
    Expected output
    4