Problem: The company X has n employees numbered from 1 through n. Each employee u has a direct boss p_u (1 ≤ p_u ≤ n), except for the employee 1 who has no boss. It is guaranteed, that values p_i form a tree. Employee u is said to be in charge of employee v if u is the direct boss of v or there is an employee w such that w is in charge of v and u is the direct boss of w. Also, any employee is considered to be in charge of himself.

In addition, for each employee u we define it's level lv(u) as follow: 

  * lv(1)=0 
  * lv(u)=lv(p_u)+1 for u ≠ 1 



In the near future, there are q possible plans for the company to operate. The i-th plan consists of two integers l_i and r_i, meaning that all the employees in the range [l_i, r_i], and only they, are involved in this plan. To operate the plan smoothly, there must be a project manager who is an employee in charge of all the involved employees. To be precise, if an employee u is chosen as the project manager for the i-th plan then for every employee v ∈ [l_i, r_i], u must be in charge of v. Note, that u is not necessary in the range [l_i, r_i]. Also, u is always chosen in such a way that lv(u) is as large as possible (the higher the level is, the lower the salary that the company has to pay the employee).

Before any plan is operated, the company has JATC take a look at their plans. After a glance, he tells the company that for every plan, it's possible to reduce the number of the involved employees exactly by one without affecting the plan. Being greedy, the company asks JATC which employee they should kick out of the plan so that the level of the project manager required is as large as possible. JATC has already figured out the answer and challenges you to do the same.

Input

The first line contains two integers n and q (2 ≤ n ≤ 100 000, 1 ≤ q ≤ 100 000) — the number of employees and the number of plans, respectively.

The second line contains n-1 integers p_2, p_3, ..., p_n (1 ≤ p_i ≤ n) meaning p_i is the direct boss of employee i.

It is guaranteed, that values p_i form a directed tree with the root of 1.

Each of the following q lines contains two integers l_i and r_i (1 ≤ l_i<r_i ≤ n) — the range of the employees, involved in the corresponding plan.

Output

Print q lines, each containing two integers — the number of the employee which should be kicked from the corresponding plan and the maximum possible level of the project manager in that case.

If there are more than one way to choose that employee, print any of them.

Example

Input

11 5
1 1 3 3 3 4 2 7 7 6
4 6
4 8
1 11
9 11
8 11


Output

4 1
8 1
1 0
11 3
8 1

Note

In the example: 

<image> In the first query, we can choose whether 4 or 5 or 6 and the project manager will be 3.

In the second query, if we choose any employee other than the employee 8, the project manager will be 1. If we choose 8, the project manager will be 3. Since lv(3)=1 > lv(1)=0, choosing 8 is the best strategy.

In the third query, no matter how we choose the employee, the project manager will always be 1.

In the fourth query, if we choose 9 or 10 then the project manager will be 3. If we choose 11 then the project manager will be 7. Since lv(7)=3>lv(3)=1, we choose 11 as the answer.

Solution (in JAVA):

Solution:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;

public class D {

    static ArrayList<Integer>[] adj;
    static int tin[];
    static int[] lvl;
    static int[] lcaArr;
    static int[] par;
    static HashMap<Integer, Integer> map;

    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        PrintWriter pw = new PrintWriter(System.out);

        map = new HashMap<>();
        int n = sc.nextInt();
        N = n+1;
        int q = sc.nextInt();
        adj = new ArrayList[n+1];
        for (int i = 0; i <= n; i++) {
            adj[i] = new ArrayList<>();
        }
        tin = new int[n+1];
        for (int i = 2; i <= n; i++) {
            adj[sc.nextInt()].add(i);
        }

        lvl = new int[n+1];
        par = new int[n+1];
        lcaArr = new int[2*n+100];
        Arrays.fill(lcaArr, 1000000000);
        dfs(1, 0);


        int N = 1; while(N < n) N <<= 1; //padding

        int[] in = new int[N + 1];
        for(int i = 1; i <= n; i++)
                in[i] = tin[i];

        SegmentTree st = new SegmentTree(in);
        L = lvl;
        preprocessParents(par);

        while(q-->0){
            int a = sc.nextInt();
            int b = sc.nextInt();
            int[][] query = st.query(a, b);
            int min1 = map.get(query[0][0]);
            int min2 = map.get(query[0][1]);
            int max1 = map.get(query[1][0]);
            int max2 = map.get(query[1][1]);

            int lvlDelmin1 = lvl[query(max2, min2)];
            int lvlDelmax2 = lvl[query(min1, max1)];

            if(lvlDelmin1 > lvlDelmax2)
            {
                pw.println(min1 + " " + lvlDelmin1);
            }else{
                pw.println(max2 + " " + lvlDelmax2);
            }
        }


        pw.flush();
        pw.close();
    }

    static int time = 0;
    static void dfs(int u, int l){
        tin[u] = ++time;
        map.put(tin[u], u);
        lvl[u] = l;

        for (int v: adj[u]) {
            par[v] = u;
            dfs(v, l+1);
        }
    }




    static int N, L[], P[][];			// P[i][j] --> the 2^j th ancestor of node i

    static void preprocessParents(int[] T)	// O(n log n)
    {
        int k = 0; while(1<<k+1 < N) ++k;		// max parent = floor(log2(N - 1))

        P = new int[N][k + 1];

        for(int i = 0; i < N; i++)
            Arrays.fill(P[i], -1);

        // base case: direct parents
        for(int i = 0; i < N; ++i)
            P[i][0] = T[i];					// T[root] = -1

        for(int j = 1; j <= k; j++)
            for(int i = 0; i < N; i++)
                if(P[i][j-1] != -1)
                    P[i][j] = P[P[i][j-1]][j-1];
    }

    static int query(int p, int q)		// O(log n)
    {
        //if p is situated on a higher level than q, swap them
        if (L[p] < L[q]) { p ^= q; q ^= p; p ^= q; }

        //find largest k such that 2^k is a parent of p
        int k = 0;
        while(1<<k+1 <= L[p])
            ++k;

        //find the ancestor of p situated on the same level with q
        for (int i = k; i >= 0; --i)
            if (L[p] - (1<<i) >= L[q])
                p = P[p][i];

        if (p == q)
            return p;

        //go up to lowest (non-common) ancestors for p and q
        for (int i = k; i >= 0; --i)
            if (P[p][i] != -1 && P[p][i] != P[q][i])
            {
                p = P[p][i]; q = P[q][i];
            }

        return P[p][0];
    }


//    static int zero = tin[0];
//    static int inf = tin[tin.length-1];









    static  class SegmentTree {		// 1-based DS, OOP

        int N; 			//the number of elements in the array as a power of 2 (i.e. after padding)
        int[] array;
        int[][] minstree;
        int[][] maxstree;

        SegmentTree(int[] in)
        {
            array = in; N = in.length - 1;
            minstree = new int[N<<1][2];		//no. of nodes = 2*N - 1, we add one to cross out index zero
            maxstree = new int[N<<1][2];		//no. of nodes = 2*N - 1, we add one to cross out index zero
            build(1,1,N);
        }

        void build(int node, int b, int e)	// O(n)
        {
            if(b == e) {
//                sTree[node] = array[b];
                minstree[node] = new int[]{array[b], 1000000000};
                maxstree[node] = new int[]{-1, array[b]};
            }
            else
            {
                int mid = b + e >> 1;
                build(node<<1,b,mid);
                build(node<<1|1,mid+1,e);
//                sTree[node] = sTree[node<<1]+sTree[node<<1|1];
                minstree[node] = mergeMin(minstree[node<<1], minstree[node<<1|1]);
                maxstree[node] = mergeMax(maxstree[node<<1], maxstree[node<<1|1]);
            }
        }

        int[] mergeMin(int[] a, int[] b){
            int[] tot = new int[]{a[0], a[1], b[0], b[1]};
            Arrays.sort(tot);
            return new int[]{tot[0], tot[1]};
        }

        int[] mergeMax(int[] a, int[] b){
            int[] tot = new int[]{a[0], a[1], b[0], b[1]};
            Arrays.sort(tot);
            return new int[]{tot[2], tot[3]};
        }

        int[][] query(int i, int j)
        {
            return query(1,1,N,i,j);
        }

        // [[min],[max]]
        int[][] query(int node, int b, int e, int i, int j)	// O(log n)
        {
            if(i>e || j <b)
                return new int[][]{{1000000000, 1000000000}, {1, 1}};
            if(b>= i && e <= j) {
                return new int[][]{minstree[node], maxstree[node]};
            }
            int mid = b + e >> 1;
            int[][] q1 = query(node<<1,b,mid,i,j);
            int[][] q2 = query(node<<1|1,mid+1,e,i,j);
            return new int[][]{mergeMin(q1[0], q2[0]), mergeMax(q1[1], q2[1])};

        }


//        public static void main(String[] args) {
//
//            Scanner sc = new Scanner(System.in);
//
//            int n = sc.nextInt();
//            int N = 1; while(N < n) N <<= 1; //padding
//
//            int[] in = new int[N + 1];
//            for(int i = 1; i <= n; i++)
//                in[i] = sc.nextInt();
//
//            sc.close();
//        }

    }

    static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}