package org.pilgrim.hiredintech;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;

public class GravityTree
{
    static Map<Integer, TreeNode> map = new HashMap<>();

    public static TreeNode buildTree(int[] a)
    {
        TreeNode root = null;
        for (int i = 0; i < a.length; i++)
        {
            int val = a[i];
            TreeNode treeNode = map.get(val);
            if (null == treeNode)
            {
                treeNode = new TreeNode();
                treeNode.val = val;
                map.put(val, treeNode);
            }

            int childVal = i + 2;
            TreeNode treeChild = map.get(childVal);
            if (null == treeChild)
            {
                treeChild = new TreeNode();
                treeChild.val = childVal;
                map.put(childVal, treeChild);
            }
            if (null != treeNode)
            {
                treeNode.add(treeChild);
            }

            root = treeNode;
        }

        while (null != root && null != root.parent)
        {
            root = root.parent;
        }
        return root;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n - 1];
        for (int i = 0; i < n - 1; i++)
        {
            a[i] = in.nextInt();
        }
        int q = in.nextInt();

        int[][] exp = new int[q][2];
        for (int i = 0; i < q; i++)
        {
            exp[i][0] = in.nextInt();
            exp[i][1] = in.nextInt();
        }

        in.close();

        TreeNode root = buildTree(a);

        for (int i = 0; i < exp.length; i++)
        {
            int u = exp[i][0];
            int v = exp[i][1];

            long forces = 0;

            TreeNode nodeV = map.get(v);
            TreeNode nodeU = map.get(u);
            nodeV.on();
            ///////////////////
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(nodeV);
            while (!queue.isEmpty())
            {
                TreeNode from = queue.poll();
                queue.addAll(from.children);

                //////////////////////////////////////////////////
                long distance = getDistance(from, nodeU);
                forces += distance * distance;
            }
            System.out.println(forces);

            ///////////////////
            nodeV.off();
        }
    }

    private static long getDistance(TreeNode from,
                                    TreeNode nodeU)
    {
        long height1 = getHeight(from);
        long height2 = getHeight(nodeU);

        long diff = height1 - height2;
        if (diff < 0)
        {
            diff = Math.abs(diff);
            nodeU = rise(nodeU, diff);
        }
        else if (diff > 0)
        {
            diff = Math.abs(diff);
            from = rise(from, diff);
        }
        diff = Math.abs(diff);

        while (from != nodeU)
        {
            from = from.parent;
            nodeU = nodeU.parent;
            diff += 2;
        }

        return diff;
    }

    private static TreeNode rise(TreeNode from,
                                 long diff)
    {
        for (int i = 0; i < diff; i++)
        {
            from = from.parent;
        }
        return from;
    }

    private static long getHeight(TreeNode from)
    {
        long height = 0;
        while (null != from && null != from.parent)
        {
            from = from.parent;
            height++;
        }
        return height;
    }
}

class TreeNode
{
    List<TreeNode> children = new ArrayList<>();
    TreeNode       parent   = null;
    long           val;
    boolean        on       = false;

    public boolean isOn()
    {
        return on;
    }

    public void on()
    {
        this.on = true;
    }

    public void off()
    {
        this.on = false;
    }

    public TreeNode add(TreeNode treeNode)
    {
        children.add(treeNode);
        treeNode.parent = this;
        return this;
    }
}

@@ -252,15 +252,12 @@
 Map%3C
-Integer
+Long
 , Tr
@@ -325,19 +325,20 @@
 ildTree(
-int
+long
 %5B%5D a)%0A  
@@ -436,19 +436,20 @@
         
-int
+long
  val = a
@@ -693,19 +693,20 @@
         
-int
+long
  childVa
@@ -1371,19 +1371,20 @@
         
-int
+long
 %5B%5D a = n
@@ -1386,19 +1386,20 @@
 a = new 
-int
+long
 %5Bn - 1%5D;
@@ -1467,35 +1467,36 @@
   a%5Bi%5D = in.next
-Int
+Long
 ();%0A        %7D%0A  
@@ -1532,19 +1532,20 @@
         
-int
+long
 %5B%5D%5B%5D exp
@@ -1551,19 +1551,20 @@
 p = new 
-int
+long
 %5Bq%5D%5B2%5D;%0A
@@ -1632,35 +1632,36 @@
 %5Bi%5D%5B0%5D = in.next
-Int
+Long
 ();%0A            
@@ -1671,35 +1671,36 @@
 %5Bi%5D%5B1%5D = in.next
-Int
+Long
 ();%0A        %7D%0A%0A 
@@ -1825,19 +1825,20 @@
         
-int
+long
  u = exp
@@ -1857,19 +1857,20 @@
         
-int
+long
  v = exp
@@ -3538,16 +3538,129 @@
 ht = 0;%0A
+        if (null != from && null != from.height)%0A        %7B%0A            return from.height.longValue();%0A        %7D%0A
         
@@ -3767,16 +3767,138 @@
 ight++;%0A
+            if (null != from.height)%0A            %7B%0A                return from.height.longValue() + height;%0A            %7D%0A
         
@@ -4093,16 +4093,43 @@
 = false;
+%0A    Long           height;
 %0A%0A    pu

change int to long