This page is still under construction.

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

Erdős Numbers

Time limit5sMemory limit128 MB

Summary
Given papers with years and authors, find the shortest chain from Erdos to a person using strictly increasing years up to a query year.
Level

Medium7 of 10

Topics
Graph, BFS, Sorting, Shortest path
Solved
No attempts yet

Statement

Among researchers there is a well-known concept called the Erdős number, named after the famous mathematician Paul Erdős. A person's Erdős number is the smallest value determined by the following rules:

  • Erdős has Erdős number 0.
  • If you have co-authored a paper with Erdős, your Erdős number is 1.
  • If you have co-authored a paper with someone whose Erdős number is 1, your Erdős number is 2, and so on.

If none of the rules apply, the Erdős number is infinite. A smaller Erdős number reflects a closer co-authorship link to Erdős.

However, this classic definition ignores the passage of time. For example, suppose Erdős co-authored a paper with A, and A co-authored a paper with B. Then A gets 1 and B gets 2. But A may have written the paper with B early in their career and the paper with Erdős much later. Is it then fair for B to already have 2? The classic definition only counts the length of the shortest chain of papers linking a person to Erdős.

To account for the effect of time, we add the following requirement: the publication years along a chain that starts from Erdős must form a strictly increasing sequence. That is, each next paper in the chain must have been published in a strictly later year than the previous one.

Given a list of publications, answer queries of the form: as of a given year, what is the Erdős number of a given person? under the requirement above. A person's Erdős number as of year YY is the length (number of papers) of the shortest valid chain from Erdős to that person that uses only papers published in year YY or earlier and whose years strictly increase. If no such chain exists, the answer is infinite.

Input

The first line contains an integer zz — the number of data sets that follow.

Each data set is described as follows:

  • The first line contains two integers pp and qq (1≤p,q≤1000001 \le p, q \le 100000): the number of publications and the number of queries.
  • Each of the next pp lines describes one publication: an integer yy (1913≤y≤20051913 \le y \le 2005), the publication year, followed by the space-separated surnames of all of its (distinct) co-authors. Each publication has between 22 and 1010 authors. A surname is a string of English letters whose first letter is uppercase and the rest lowercase, with length between 11 and 1010.
  • Each of the next qq lines describes one query: an integer yy, the year being asked about, followed by a space and the surname of a person (same surname format as above).

The number of distinct authors in a data set does not exceed 100000100000. Paul Erdős appears in the input under the surname Erdos.

Output

For each data set, print qq lines, one per query in the given order. For each query, print the Erdős number of the given person as of the queried year. If that Erdős number is infinite, print NIESKONCZONA (the Polish word for "infinite").

Examples2

  1. Example 1

    Input
    1
    3 4
    1943 Erdos Tarski
    1929 Kuratowski Tarski
    1971 Henkin Monk Tarski
    1929 Kuratowski
    1980 Monk
    1929 Monk
    1943 Tarski
    
    Expected output
    NIESKONCZONA
    2
    NIESKONCZONA
    1
    
  2. Example 2

    Input
    1
    1 3
    2000 Erdos Alpha
    1913 Erdos
    2000 Alpha
    1999 Alpha
    
    Expected output
    0
    1
    NIESKONCZONA