#include <bits/stdc++.h>

#define xx first
#define yy second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mp make_pair
#define pb push_back
#define popb pop_back

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef string str;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX=1e6+3;
int n, m, a, b;
vector<int> g[MAX];
bool v[MAX];
int d[MAX];
int ord[MAX];

void dfs(int node, int depth){
    v[node]=1;
    d[node]=depth;
    for(auto x : g[node]) if(!v[x]) dfs(x, depth+1);
}

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

    cin >> n;
    for(int i=0;i<n;++i){
        cin >> ord[i];
        ord[i]--;
    }
    for(int i=1;i<n;++i){
        cin >> a >> b;
        --a; --b;
        g[a].pb(b);
        g[b].pb(a);
    }
    int q; cin >> q;
    while(q--){
        int nista, l, r, root, maxi=0;
        cin >> nista >> l >> r >> root;
        dfs(root-1, 0);
        for(int i=0;i<n;++i) v[i]=0;
        for(int i=l;i<=r;++i){
            maxi=max(maxi, d[ord[i]]);
        }
        cout << maxi << '\n';
    }

    return 0;
}