#include <bits/stdc++.h>
using namespace std;
int bs(int a[],int n,int val)
{
	if(a[0]>val) return -1;
	if(a[n-1]<=val) return n-1;
	int l=0;int u=n-2;
	while(l<=u)
	{
	 int mid=(l+u)/2;	
	 if(a[mid]<=val && a[mid+1]>val)
	  return mid;
	 else if(a[mid]>val)
	  u=mid-1;
	 else l=mid+1;
	}
}
int main() {
	int n,m;
	cin>>n>>m;
    int a[n];
    for(int i=0;i<n;i++)
     cin>>a[i];
    sort(a,a+n); 
    int p[n];
    int nn[n];
    nn[n-1]=-a[n-1];
    p[n-1]=a[n-1];
    for(int i=n-2;i>=0;i--)
    {
    	p[i]=a[i]+nn[i+1];
    	nn[i]=-a[i]+p[i+1];
    }
    int pf[n];
    int nnf[n];
    nnf[0]=-a[0];
    pf[0]=a[0];
    for(int i=1;i<n;i++)
    {
    	pf[i]=a[i]+nnf[i-1];
    	nnf[i]=-a[i]+pf[i-1];
    }    
    int mini=INT_MAX;
    for(int i=0;i<m;i++)
    {
    	int val;cin>>val;
    	int x=bs(a,n,val);//x is last element less than , equal to val
    	//cout<<x<<endl;
    	int ans=INT_MAX;
    	if((x+1)%2==0)
    	 {
    	 	int a=0,b=0;
    	 	if(x!=-1) a=pf[x];
    	 	if(x+1<n) b=p[x+1];
    	    ans=a-val+b;
    	 } 
    	else 
    	{
    	 	int a=0,b=0;
    	 	if(x!=-1) a=nnf[x];
    	 	if(x+1<n) b=nn[x+1];
    	    ans=a+val+b;    		
    	} 
    	mini=min(mini,ans); 
    }
    cout<<mini<<endl;
	return 0;
}
#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}
const int MAX = 1000;
char C[MAX * 4];
const int dm = 1 << 17;
char* dn = cn, * di = dn, * owad = dn + dm - 20;
void putint(int A) {
	if (owad < di) {
		fwrite(dn, 1, di - dn, stdout);
		di = dn;
	}
	if (A >= 1000) {
		int dig = 1;
		if (A >= 100000) dig = 3;
		else if (A >= 10000) dig = 2;
		memcpy(di, C + A / 1000 * 4 + 3 - dig, dig);
		memcpy(di + dig, C + A % 1000 * 4, 4);
		di += dig + 4;
	}
	else {
		int dig = 1;
		if (A >= 100) dig = 3;
		else if (A >= 10) dig = 2;
		memcpy(di, C + A * 4 + 3 - dig, dig + 1);
		di += dig + 1;
	}
}

int N, Q;
int D[200001], dmemo[200001];
int ne[200001], he[200001];
int ne2[200001], he2[200001];
int dcnt[200001];
int que[200001], q, d;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	N = getint();
	for (int i = 2; i <= N; i++) {
		int a = getint();
		ne[i] = he[a];
		he[a] = i;
	}
	Q = getint();
	rep1(q, Q) {
		int u = getint();
		ne2[q] = he2[u];
		he2[u] = q;
		D[q] = getint();
	}
	que[q++] = 1;
	while (q) {
		int p = que[q - 1];
		if (p > 0) {
			que[q - 1] = -p;
			for (int i = he2[p]; i; i = ne2[i]) {
				dmemo[i] = dcnt[D[i]];
			}
			dcnt[d]++;
			d++;
			for (int i = he[p]; i; i = ne[i]) {
				que[q++] = i;
			}
		}
		else {
			for (int i = he2[-p]; i; i = ne2[i]) {
				dmemo[i] = dcnt[D[i]] - dmemo[i];
			}
			d--;
			q--;
		}
	}

	rep(i, MAX) {
		int X = i;
		rep(j, 3) {
			C[i * 4 + 2 - j] = '0' + X % 10;
			X /= 10;
		}
		C[i * 4 + 3] = ' ';
	}
	rep1(i, Q) putint(dmemo[i]);
	fwrite(dn, 1, di - dn, stdout);

	Would you please return 0;
}