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.*;

public class CFB {
    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;
    private static final long MOD = 1000L * 1000L * 1000L + 7;
    private static final int[] dx = {0, -1, 0, 1};
    private static final int[] dy = {1, 0, -1, 0};
    private static final String yes = "Yes";
    private static final String no = "No";

    int n;
    List<List<Integer>> graph = new ArrayList<>();
    int INDEX = 0;
    int[] start;
    int[] end;
    int[] minTree;
    int[] maxTree;
    // LCA algorithm:
    int LG = 20;
    int[] level;
    int[][] dp;
    int[] layer;
    void build_tree(int node, int nodeStartIndex, int nodeEndIndex) {
        if(nodeStartIndex > nodeEndIndex) return; // Out of range
        if(nodeStartIndex == nodeEndIndex) { // Leaf node
            minTree[node] = nodeStartIndex; // Init value
            maxTree[node] = nodeStartIndex;
            return;
        }

        build_tree(node * 2, nodeStartIndex, (nodeStartIndex + nodeEndIndex) / 2); // Init left child
        build_tree(node * 2 + 1, 1 + (nodeStartIndex + nodeEndIndex) / 2, nodeEndIndex); // Init right child
        minTree[node] = start[minTree[node * 2]] < start[minTree[node * 2 + 1]] ? minTree[2 * node] : minTree[2 * node + 1];
        maxTree[node] = start[maxTree[node * 2]] > start[maxTree[node * 2 + 1]] ? maxTree[2 * node] : maxTree[2 * node + 1];
    }

    /**
     * Query tree to get max element value within range [queryStartIndex, queryEndIndex]
     */
    int query_mintree(int node, int nodeStartIndex, int nodeEndIndex, int queryStartIndex, int queryEndIndex) {
        if(nodeStartIndex > nodeEndIndex || nodeStartIndex > queryEndIndex || nodeEndIndex < queryStartIndex) return -1; // Out of range
        if(nodeStartIndex >= queryStartIndex && nodeEndIndex <= queryEndIndex) // Current segment is totally within range [i, j]
            return minTree[node];
        int q1 = query_mintree(node * 2, nodeStartIndex, (nodeStartIndex + nodeEndIndex) / 2, queryStartIndex, queryEndIndex); // Query left child
        int q2 = query_mintree(1 + node * 2, 1 + (nodeStartIndex + nodeEndIndex) / 2, nodeEndIndex, queryStartIndex, queryEndIndex); // Query right child
        if (q1 == -1) {
            return q2;
        }
        if (q2 == -1) {
            return q1;
        }

        return start[q1] < start[q2] ? q1 : q2;
    }

    /**
     * Query tree to get max element value within range [queryStartIndex, queryEndIndex]
     */
    int query_maxtree(int node, int nodeStartIndex, int nodeEndIndex, int queryStartIndex, int queryEndIndex) {
        if(nodeStartIndex > nodeEndIndex || nodeStartIndex > queryEndIndex || nodeEndIndex < queryStartIndex) return -1; // Out of range
        if(nodeStartIndex >= queryStartIndex && nodeEndIndex <= queryEndIndex) // Current segment is totally within range [i, j]
            return maxTree[node];
        int q1 = query_maxtree(node * 2, nodeStartIndex, (nodeStartIndex + nodeEndIndex) / 2, queryStartIndex, queryEndIndex); // Query left child
        int q2 = query_maxtree(1 + node * 2, 1 + (nodeStartIndex + nodeEndIndex) / 2, nodeEndIndex, queryStartIndex, queryEndIndex); // Query right child
        if (q1 == -1) {
            return q2;
        }
        if (q2 == -1) {
            return q1;
        }

        return start[q1] > start[q2] ? q1 : q2;
    }

    void solve() throws IOException {
        n = nextInt();
        int Q = nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 2; i <= n; i++) {
            int pi = nextInt() - 1;
            graph.get(pi).add(i - 1);
            graph.get(i - 1).add(pi);
        }

        layer = new int[100000 + 10];
        start = new int[n];
        end = new int[n];
        dfs(-1, 0, start, end, 0);

        level = new int[n];
        dp = new int[n][LG];
        for (int i = 0; i < n; i++) {
            Arrays.fill(dp[i], -1);
        }

        dfs(0, -1);
        for(int i = 1; i < LG; i++){
            for (int j = 0; j < n; j++) {
                if (dp[j][i - 1] != -1) {
                    dp[j][i] = dp[dp[j][i - 1]][i - 1];
                }
            }
        }

        minTree = new int[5 * n];
        maxTree = new int[5 * n];
        build_tree(1, 0, n - 1);

        for (int q = 0; q < Q; q++) {
            int l = nextInt() - 1;
            int r = nextInt() - 1;

            int idmin = query_mintree(1, 0, n - 1, l, r);
            int idmax = query_maxtree(1, 0, n - 1, l, r);
            int res1 = getlca(l, r, idmin);
            int res2 = getlca(l, r, idmax);
            if (res1 < res2) {
                outln((1 + idmax) + " " + res2);
            }
            else {
                outln((1 + idmin) + " " + res1);
            }
        }
    }

    int getlca(int l, int r, int ignore) {
        int res = 0;
        if (ignore == l) {
            res = layer[lcaRange(l + 1, r)];
        }
        else {
            if (ignore == r) {
                res = layer[lcaRange(l, r - 1)];
            }
            else {
                int l1 = lcaRange(l, ignore - 1);
                int l2 = lcaRange(ignore + 1, r);
                res = layer[lca(l1, l2)];
            }
        }

        return res;
    }

    int lcaRange(int lower, int upper) {
        int idmin = query_mintree(1, 0, n - 1, lower, upper);
        int idmax = query_maxtree(1, 0, n - 1, lower, upper);
        return lca(idmin, idmax);
    }

    void dfs(int u, int par){
        level[u] = par == -1 ? 0 : 1 + level[par];
        dp[u][0] = par;

        for(int v : graph.get(u)){
            if (v == par) continue;
            dfs(v, u);
        }
    }

    int lca(int u, int v){
        if (level[u] < level[v]) {
            int tmp = u;
            u = v;
            v = tmp;
        }

        int lg = 0;
        while ((1 << lg) <= level[u]) {
            lg++;
        }
        lg--;

        for(int i = lg; i >= 0; i--){
            if (level[u] - (1 << i) >= level[v]) {
                u = dp[u][i];
            }
        }

        if (u == v) {
            return u;
        }

        for(int i = lg; i >= 0; i--){
            if (dp[u][i] != -1 && dp[u][i] != dp[v][i]) {
                u = dp[u][i];
                v = dp[v][i];
            }
        }

        return dp[u][0];
    }

    //make sure u has a deeper depth.
    int dis(int u, int v){
        if (level[u] < level[v]) {
            int tmp = u;
            u = v;
            v = tmp;
        }

        int w = lca(u, v);
        return level[u] + level[v] - 2 * level[w];
    }

    int getAncestor(int u, int dis){
        dis = level[u] - dis;
        int lg = 0;
        while ((1 << lg) <= level[u]) {
            lg++;
        }
        lg--;

        for(int i =lg; i >= 0; i--) {
            if (level[u] - (1 << i) >= dis) {
                u = dp[u][i];
            }
        }

        return u;
    }

    // Euler's tour:
    void dfs(int parent, int nodeIndex, int[] startIndex, int[] endIndex, int lay) {
        startIndex[nodeIndex] = INDEX;
        INDEX++;
        layer[nodeIndex] = lay;
        for (int nxt : graph.get(nodeIndex)) {
            if (nxt != parent) {
                dfs(nodeIndex, nxt, startIndex, endIndex, lay + 1);
            }
        }

        endIndex[nodeIndex] = INDEX - 1;
    }

    void shuffle(int[] a) {
        int n = a.length;
        for(int i = 0; i < n; i++) {
            int r = i + (int) (Math.random() * (n - i));
            int tmp = a[i];
            a[i] = a[r];
            a[r] = tmp;
        }
    }
    long gcd(long a, long b) {
        while(a != 0 && b != 0) {
            long c = b;
            b = a % b;
            a = c;
        }
        return a + b;
    }
    private void outln(Object o) {
        out.println(o);
    }
    private void out(Object o) {
        out.print(o);
    }
    private void formatPrint(double val) {
        System.out.format("%.9f%n", val);
    }
    public CFB() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }
    public static void main(String[] args) throws IOException {
        new CFB();
    }

    public long[] nextLongArr(int n) throws IOException{
        long[] res = new long[n];
        for(int i = 0; i < n; i++)
            res[i] = nextLong();
        return res;
    }
    public int[] nextIntArr(int n) throws IOException {
        int[] res = new int[n];
        for(int i = 0; i < n; i++)
            res[i] = nextInt();
        return res;
    }
    public String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }
    public String nextString() {
        try {
            return br.readLine();
        } catch (IOException e) {
            eof = true;
            return null;
        }
    }
    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }
    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }
}