This page is still under construction.

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

Unter

Time limit1sMemory limit1024 MB

Summary
A connected graph with N houses and exactly N edges (one cycle) must answer up to 1e6 shortest distance queries.
Level

Hard8 of 10

Topics
Graph, DFS, Tree, Prefix sum
Solved
No attempts yet

Problem

Justas plans to build an app that lets people share car rides. First, he needs to write a program that finds the shortest distance between two houses.

In the city where the app will run there are NN houses, numbered from 11 to NN. The houses are directly connected by NN bidirectional streets. Each street connects exactly two houses, and any two houses are connected by at most one street.

Justas has already written an algorithm that finds the shortest path between two houses when there is only one way to get from one house to another without passing through the same house more than once. But he needs your help for pairs of houses for which more than one such way exists.

Find the shortest distance for QQ pairs of houses.

Input

The first line contains two integers: the number of houses NN and the number of queries QQ.

Each of the next NN lines contains two space-separated integers aia_i and bib_i, meaning there is a street between houses aia_i and bib_i.

Each of the remaining QQ lines contains two space-separated integers cjc_j and djd_j.

Output

Print QQ lines. On the kk-th line, print a single integer — the length of the shortest path between houses ckc_k and dkd_k. Justas measures the distance between two houses as the number of streets that must be traveled.

Constraints

  • 3≤N≤2000003 \le N \le 200000
  • 1≤ai,bi≤N1 \le a_i, b_i \le N (1≤i≤N, ai≠bi)(1 \le i \le N,\ a_i \ne b_i)
  • 1≤Q≤10000001 \le Q \le 1000000
  • 1≤cj,dj≤N1 \le c_j, d_j \le N (1≤j≤Q, cj≠dj)(1 \le j \le Q,\ c_j \ne d_j)
  • From cjc_j to djd_j there is more than one path (not necessarily the shortest) that does not pass through any house more than once.
  • From any house you can always reach any other house via the streets.

Examples5

  1. Example 1

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

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

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

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

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