This page is still under construction.

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

Arrays

Time limit4sMemory limit128 MB

Summary
Given two n by m arrays of distinct integers, decide whether one can be turned into the other by permuting rows and permuting columns.
Level

Medium6 of 10

Topics
Hash map, Sorting, Implementation
Solved
No attempts yet

Problem

You are given an array of size n×mn \times m filled with pairwise distinct integers. The following two operations may be applied to the array:

  1. interchange two rows,
  2. interchange two columns.

Two arrays are called alike if one of them can be obtained from the other by a sequence of the operations above.

Given several pairs of arrays, write a program that determines, for each pair, whether the two arrays are alike.

Input

The first line contains one integer tt (1≤t≤101 \le t \le 10), the number of array pairs. Descriptions of the pairs follow.

Each description begins with a line containing two integers nn and mm (1≤n,m≤10001 \le n, m \le 1000), separated by a single space, the common number of rows and columns of the two arrays.

The next nn lines describe the first array; the ii-th of them contains mm integers aija_{ij} (−106≤aij≤106-10^6 \le a_{ij} \le 10^6), separated by single spaces, the values in the ii-th row of the first array.

The following nn lines describe the second array in the same way; the integers in its ii-th line are denoted bijb_{ij}.

All numbers within a single array are pairwise distinct.

Output

Print tt lines, one per pair. The kk-th line should contain TAK if the two arrays of the kk-th pair are alike, or NIE otherwise. (TAK and NIE mean "yes" and "no", respectively.)

Note

Interchanging rows or columns preserves the relations "lies in the same row" and "lies in the same column". Hence, for two arrays to be alike, they must first contain the same set of values, and any values sharing a row (or a column) in one array must also share a row (or a column) in the other. Checking whether the row-to-row and column-to-column correspondences can be fixed without contradiction settles whether the arrays are alike.

Examples3

  1. Example 1

    Input
    2
    4 3
    1 2 3
    4 5 6
    7 8 9
    10 11 12
    11 10 12
    8 7 9
    5 4 6
    2 1 3
    2 2
    1 2
    3 4
    5 6
    7 8
    
    Expected output
    TAK
    NIE
    
  2. Example 2

    Input
    1
    1 1
    42
    42
    
    Expected output
    TAK
    
  3. Example 3

    Input
    1
    1 1
    42
    7
    
    Expected output
    NIE