Under construction

Still being built. Things may change

Brothers in Arms

Interview

Time limit10sMemory limit1024 MB

Summary
Each city is an edge between its top and bottom symbols. For each query, find the shortest friendship chain length between two cities, or -1 if none exists.
Level

Medium5 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Problem

In medieval times, keeping track of relationships between cities was extremely difficult, since most cities did not have access to the internet[citation needed]. However, it was possible to determine whether two cities were friendly by examining their coats of arms. Every coat of arms showed two symbols: one at the top and one at the bottom. If two cities have an equal symbol at the top, or an equal symbol at the bottom, they are friendly.

Following the saying "the friends of my friends are my friends", two cities c_0c\_0 and c_fc\_f are indirectly friendly if there exist cities c_1,…,c_f−1c\_1, \ldots, c\_{f-1} such that c_kc\_k is friendly with c_k+1c\_{k+1} for 0≤k<f0\leq k < f. If c_0c\_0 and c_fc\_f are different and indirectly friendly, then the friendship degree of these cities is the smallest possible ff following this definition. See Figure B.1 for an example.

Parts of these coats of arms are CC BY-SA 4.0 on Wikimedia Commons.

Figure B.1: Illustration of Sample Input 1. Cities 1 and 2 are directly friendly, as are cities 2 and 3. Cities 1 and 3 have a friendship degree of 2, because they are indirectly friendly via city 2. City 4 is not (indirectly) friendly with any other city.

You are given a list of coats of arms and a list of queries. For every query, determine the friendship degree of the two given cities.

Input

The input consists of:

One line with two integers nn and ss (2≤n≤90 0002\leq n\leq 90\,000, 2≤s≤3002\leq s\leq 300), the number of cities and the number of symbols that may appear on the coat of arms of some city.

nn lines, the iith of which consists of two integers tit_i and bib_i (1≤ti,bi≤s1\leq t_i, b_i\leq s). tit_i is the symbol on the top side of the coat of arms of the iith city, and bib_i is the symbol on the bottom side. If i≠ji\neq j, then ti≠tjt_i\neq t_j or bi≠bjb_i\neq b_j.

One line with an integer qq (1≤q≤1051\leq q\leq 10^5), the number of queries.

qq lines, the iith of which contains two integers cc and dd (1≤c,d≤n1\leq c, d\leq n, c≠dc\neq d), two cities for which you should calculate the friendship degree.

Output

For every query, output an integer stating the friendship degree of the two cities, or −1-1 if the two cities are not (indirectly) friendly.

Examples3

  1. Example 1

    Input
    4 5
    1 2
    3 2
    3 4
    5 1
    4
    1 2
    2 3
    1 3
    1 4
    
    Expected output
    1
    1
    2
    -1
    
  2. Example 2

    Input
    7 5
    1 1
    2 1
    2 2
    3 2
    4 2
    3 5
    3 4
    6
    1 2
    1 3
    1 7
    1 4
    1 5
    6 7
    
    Expected output
    1
    2
    4
    3
    3
    1
    
  3. Example 3

    Input
    2 4
    1 2
    3 4
    1
    1 2
    
    Expected output
    -1