#RELAY2B. Evergrowing Tree

Evergrowing Tree

Score : 100100 points

Problem Statement

You are given an integer NN. Consider an infinite NN-ary tree as shown below:

Figure: an infinite NN-ary tree for the case N=3N = 3

As shown in the figure, each vertex is indexed with a unique positive integer, and for every positive integer there is a vertex indexed with it. The root of the tree has the index 11. For the remaining vertices, vertices in the upper row have smaller indices than those in the lower row. Among the vertices in the same row, a vertex that is more to the left has a smaller index.

Regarding this tree, process QQ queries. The ii-th query is as follows:

  • Find the index of the lowest common ancestor (see Notes) of Vertex viv_i and wiw_i.

Notes

  • In a rooted tree, the lowest common ancestor (LCA) of Vertex vv and ww is the farthest vertex from the root that is an ancestor of both Vertex vv and ww. Here, a vertex is considered to be an ancestor of itself. For example, in the tree shown in Problem Statement, the LCA of Vertex 55 and 77 is Vertex 22, the LCA of Vertex 88 and 1111 is Vertex 11, and the LCA of Vertex 33 and 99 is Vertex 33.

Constraints

  • 1N1091 \leq N \leq 10^9
  • 1Q1051 \leq Q \leq 10^5
  • 1vi<wi1091 \leq v_i < w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

NN QQ

v1v_1 w1w_1

::

vQv_Q wQw_Q

Output

Print QQ lines. The ii-th line (1iQ)(1 \leq i \leq Q) must contain the index of the lowest common ancestor of Vertex viv_i and wiw_i.

3 3
5 7
8 11
3 9
2
1
3

The queries in this case correspond to the examples shown in Notes.

100000 2
1 2
3 4
1
1