Under construction

Still being built. Things may change

Elevated Rails

Time limit2sMemory limit2048 MB

Summary
Given three trees on the stations, answer for each pair of stations in different trees the longest simple path possible after adding two connecting edges.
Level

Medium6 of 10

Topics
Tree, Graph, BFS, DFS
Solved
No attempts yet

Problem

Brandon and Geoffry like trains! They have been tasked with building a network of rails connecting nn train stations. Each station is on one of three islands, and each island has at least one station on it.

Brandon has set up enough connections between stations on the same island so that there is exactly one way to take a train between any two stations on that island without visiting the same station twice. However, it is not yet possible to take a train between stations on different islands.

Geoffry asks several questions about Brandon's network. Each question picks two stations on different islands and asks for the maximum number of stations that a path between them could take if Brandon adds exactly two more connections so that every station can reach every other station. Brandon is too busy with rail signals to think about connecting the islands, so he leaves all of Geoffry's questions to you.

Input

The first line contains two integers nn and qq (3≤n≤1053 \le n \le 10^5, 1≤q≤2⋅1051 \le q \le 2 \cdot 10^5), the number of stations and the number of questions.

Each of the next n−3n - 3 lines contains two integers xx and yy (1≤x<y≤n1 \le x < y \le n), meaning stations xx and yy are connected by a rail that can be used in both directions.

The current rail connections satisfy the conditions above. The nn stations can be grouped into three islands so that two stations can reach each other if and only if they are on the same island, and there is a unique path between any two stations on the same island that does not repeat any station.

Each of the next qq lines contains two integers aa and bb, asking for the maximum number of stations that could appear on a path between station aa and station bb. It is guaranteed that stations aa and bb are on different islands.

Each question is independent of the others.

Output

Output qq lines, one per question. The answer to each question is a single integer: the maximum number of stations that could appear on a path between station aa and station bb.

Examples1

  1. Example 1

    Input
    12 3
    1 2
    2 3
    2 4
    5 6
    8 9
    9 10
    9 11
    7 8
    11 12
    2 5
    11 4
    7 6
    
    Expected output
    9
    9
    10