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.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class Main {

	static class Task {
		
		int NN = 100005;
		int MOD = 1000000007;
		int INF = 2000000000;
		int [] lv = new int[NN];
		int [] inTime = new int[NN];
		int [] outTime = new int[NN];
		List<Integer> [] G = new ArrayList[NN];
		int [] Tmn = new int[NN*3];
		int [] Tmx = new int[NN*3];
		int timer = 0;
		int [][] pa = new int[NN][20];
		
		void dfs(int cur, int prev) {
			++timer;
			if(prev != -1) {
				lv[cur] = lv[prev] + 1;
			}
			inTime[cur] = timer;
			for(int adj: G[cur]) {
				if(adj == prev) {
					continue;
				}
				dfs(adj, cur);
			}
			outTime[cur] = timer;
		}
		
		void buildMn(int i, int l, int r) {
			if(l == r) {
				Tmn[i] = l;
				return;
			}
			int m = (l + r)/2;
			buildMn(i*2, l, m);
			buildMn(2*i+1, 1+m, r);
			if(inTime[Tmn[i*2]] < inTime[Tmn[1+2*i]]) {
				Tmn[i] = Tmn[i*2];
			} else {
				Tmn[i] = Tmn[1 + i*2];
			}
		}
		
		void buildMx(int i, int l, int r) {
			if(l == r) {
				Tmx[i] = l;
				return;
			}
			int m = (l + r)/2;
			buildMx(i*2, l, m);
			buildMx(2*i+1, 1+m, r);
			if(inTime[Tmx[i*2]] > inTime[Tmx[1+2*i]]) {
				Tmx[i] = Tmx[i*2];
			} else {
				Tmx[i] = Tmx[1 + i*2];
			}
		}
		
		int Qmn(int i, int l, int r, int x, int y) {
			if(x > y) {
				return 0;
			}
			if(x > r || y < l) {
				return 0;
			}
			if(x <= l && r <= y) {
				return Tmn[i];
			}
			int m = (l + r) / 2;
			int idx1 = Qmn(i*2, l, m, x, y);
			int idx2 = Qmn(1+i*2, 1+m, r, x, y);
			if(idx1 == 0) {
				return idx2;
			}
			if(idx2 == 0) {
				return idx1;
			}
			if(inTime[idx1] < inTime[idx2]) {
				return idx1;
			}
			return idx2;
		}
		
		int Qmx(int i, int l, int r, int x, int y) {
			if(x > y) {
				return 0;
			}
			if(x > r || y < l) {
				return 0;
			}
			if(x <= l && r <= y) {
				return Tmx[i];
			}
			int m = (l + r) / 2;
			int idx1 = Qmx(i*2, l, m, x, y);
			int idx2 = Qmx(1+i*2, 1+m, r, x, y);
			if(idx1 == 0) {
				return idx2;
			}
			if(idx2 == 0) {
				return idx1;
			}
			if(inTime[idx1] > inTime[idx2]) {
				return idx1;
			}
			return idx2;
		}

		int findLCA(int node1, int node2) {
			if(lv[node1] > lv[node2]) {
				int node = node1;node1=node2;node2=node;
			}
			for(int j=19;j>=0;--j) {
				if(pa[node2][j] > 0 && lv[pa[node2][j]] >= lv[node1]) {
					node2 = pa[node2][j];
				}
			}
			for(int j=19;j>=0;--j) {
				if(pa[node1][j] > 0 && pa[node1][j] != pa[node2][j]) {
					node1 = pa[node1][j];
					node2 = pa[node2][j];
				}
			}
			if(node1 == node2) {
				return node1;
			}
			return pa[node1][0];
		}
		
		public void solve(InputReader in, PrintWriter out) {
			int n = in.nextInt(), q = in.nextInt();
			for(int i=1;i<=n;++i) {
				G[i] = new ArrayList<>();
			}
			for(int i=2;i<=n;++i) {
				pa[i][0] = in.nextInt();
				G[i].add(pa[i][0]);
				G[pa[i][0]].add(i);
			}
			for(int j=1;j<20;++j) {
				for(int i=1;i<=n;++i) {
					if(pa[i][j-1] > 0) {
						pa[i][j] = pa[pa[i][j-1]][j-1];						
					}
				}
			}
			dfs(1, -1);
			buildMn(1, 1, n);
			buildMx(1, 1, n);
			while(q-- > 0) {
				int l = in.nextInt(), r = in.nextInt();
				int minNode = Qmn(1, 1, n, l, r);
				int maxNode = Qmx(1, 1, n, l, r);
				int minNode1 = Qmn(1, 1, n, l, minNode - 1);
				int minNode2 = Qmn(1, 1, n, minNode + 1, r);
				if(minNode1 == 0) {
					minNode1 = minNode2;
				}
				else if(minNode2 > 0 && inTime[minNode2] < inTime[minNode1]) {
					minNode1 = minNode2;
				}
				int maxNode1 = Qmx(1, 1, n, l, maxNode - 1);
				int maxNode2 = Qmx(1, 1, n, maxNode + 1, r);
				if(maxNode1 == 0) {
					maxNode1 = maxNode2;
				}
				else if(maxNode2 > 0 && inTime[maxNode2] > inTime[maxNode1]) {
					maxNode1 = maxNode2;
				}
				int lca1 = findLCA(minNode1, maxNode);
				int lca2 = findLCA(minNode, maxNode1);
				if(lv[lca1] > lv[lca2]) {
					out.println(minNode + " " + lv[lca1]);
				} else {
					out.println(maxNode + " " + lv[lca2]);
				}
			}
		}
		
	}
	
	static void prepareIO(boolean isFileIO) {
		Task solver = new Task();
		// Standard IO
		if(!isFileIO) {
			InputStream inputStream = System.in;
	        OutputStream outputStream = System.out;
	        InputReader in = new InputReader(inputStream);
	        PrintWriter out = new PrintWriter(outputStream);
	        solver.solve(in, out);
	        out.close();
		}
        // File IO
		else {
			String IPfilePath = System.getProperty("user.home") + "/Downloads/ip.in";
	        String OPfilePath = System.getProperty("user.home") + "/Downloads/op.out";
	        InputReader fin = new InputReader(IPfilePath);
	        PrintWriter fout = null;
	        try {
				fout = new PrintWriter(new File(OPfilePath));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
	        solver.solve(fin, fout);
	        fout.close();
		}
	}
	
	public static void main(String[] args) {
        prepareIO(false);
	}
	
	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        
        public InputReader(String filePath) {
        	File file = new File(filePath);
            try {
				reader = new BufferedReader(new FileReader(file));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            tokenizer = null;
        }
        
        public String nextLine() {
        	String str = "";
        	try {
				str = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	return str;
        }
        
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

    }

}