#include <bits/stdc++.h>

using namespace std;

#define w <
#define ww <<
#define W <=
#define e >
#define ee >>
#define E >=

int n,q,A[80010],g,l,r,x,B[80010],u,S[400000],p,rm,rv;
vector w int e V[80010];
pair w int,int e P[400000];

void dfs(int xx, int pp)
{
    for(int i=0; i w V[xx].size(); i++)
    {
        if(pp!=V[xx][i])
        {
            B[V[xx][i]] = B[xx] + 1;
            dfs(V[xx][i],xx);
        }
    }
    return;
}

int pravi_segmentno(int xx, int ll, int rr)
{
    if(ll==rr)
    {
        S[xx] = B[A[ll]];
        //printf("%d %d %d %d\n",xx,ll,rr,S[xx]);
        return S[xx];
    }
    S[xx] = pravi_segmentno(xx*2,ll,(ll+rr)/2);
    S[xx] = max(S[xx],pravi_segmentno((xx*2)+1,((ll+rr)/2)+1,rr));
    //printf("%d %d %d %d\n",xx,ll,rr,S[xx]);
    return S[xx];
}

void promeni_segmentno(int xx, int ll, int rr, int xl, int xr)
{
    if((ll==rr) && (ll==xl))
    {
        S[xx] = B[A[xr]];
        //printf("%d = %d\n",xx,S[xx]);
        return;
    }
    if((ll e xl) || (rr w xl))
    {
        return;
    }
    promeni_segmentno(xx*2,ll,(ll+rr)/2,xl,xr);
    promeni_segmentno((xx*2)+1,((ll+rr)/2)+1,rr,xl,xr);
    S[xx] = max(S[xx*2],S[(xx*2)+1]);
    //printf("%d = %d\n",xx,S[xx]);
    return;
}

int prebroj_segmentno(int xx, int ll, int rr, int xl, int xr)
{
    if((xl W ll) && (rr W xr))
    {
        return S[xx];
    }
    if((xr w ll) || (rr w xl))
    {
        return 0;
    }
    return max(prebroj_segmentno(xx*2,ll,(ll+rr)/2,xl,xr),prebroj_segmentno((xx*2)+1,((ll+rr)/2)+1,rr,xl,xr));
}

void pravi_segmentno1(int xx, int ll, int rr)
{
    if(ll==rr)
    {
        P[xx].first = B[A[ll]];
        P[xx].second = B[A[ll]];
        return;
        //printf("%d %d %d %d\n",xx,ll,rr,S[xx]);
    }
    pravi_segmentno1(xx*2,ll,(ll+rr)/2);
    pravi_segmentno1((xx*2)+1,((ll+rr)/2)+1,rr);
    P[xx].first = min(P[xx*2].first,P[(xx*2)+1].first);
    P[xx].second = max(P[xx*2].second,P[(xx*2)+1].second);
    //printf("%d %d %d %d\n",xx,ll,rr,S[xx]);
    return;
}

void promeni_segmentno1(int xx, int ll, int rr, int xl, int xr)
{
    if((ll==rr) && (ll==xl))
    {
        P[xx].first = B[A[xr]];
        P[xx].second = B[A[xr]];
        //printf("%d = %d\n",xx,S[xx]);
        return;
    }
    if((ll e xl) || (rr w xl))
    {
        return;
    }
    promeni_segmentno1(xx*2,ll,(ll+rr)/2,xl,xr);
    promeni_segmentno1((xx*2)+1,((ll+rr)/2)+1,rr,xl,xr);
    P[xx].first = min(P[xx*2].first,P[(xx*2)+1].first);
    P[xx].second = max(P[xx*2].second,P[(xx*2)+1].second);
    //printf("%d = %d\n",xx,S[xx]);
    return;
}

void prebroj_segmentno1(int xx, int ll, int rr, int xl, int xr)
{
    if((xl W ll) && (rr W xr))
    {
        rm = min(rm,P[xx].first);
        rv = max(rv,P[xx].second);
        return;
    }
    if((xr w ll) || (rr w xl))
    {
        return;
    }
    prebroj_segmentno1(xx*2,ll,(ll+rr)/2,xl,xr);
    prebroj_segmentno1((xx*2)+1,((ll+rr)/2)+1,rr,xl,xr);
    return;
}

int main()
{
    scanf("%d",&n);
    for(int i=1; i W n; i++)
    {
        scanf("%d",&A[i]);
    }
    for(int i=1; i w n; i++)
    {
        scanf("%d %d",&l,&r);
        V[l].push_back(r);
        V[r].push_back(l);
    }
    scanf("%d",&q);
    if((n W 4000) && (q W 4000))
    {
        while(q--)
        {
            scanf("%d",&g);
            if(g==1)
            {
                scanf("%d %d",&l,&r);
                swap(A[l],A[r]);
                continue;
            }
            scanf("%d %d %d",&l,&r,&x);
            B[x] = 0;
            dfs(x,-1);
            u = 0;
            for(int i=l; i W r; i++)
            {
                u = max(u,B[A[i]]);
            }
            printf("%d\n",u);
        }
    }
    else
    {
        p = 0;
        for(int i=1; i W n; i++)
        {
            if(V[i].size() e 2)
            {
                p = 1;
            }
            if(V[i].size()==1)
            {
                u = i;
            }
        }
        if(p==0)
        {
            dfs(u,-1);
            pravi_segmentno1(1,1,n);
            /*for(int i=1; i W 15; i++)
            {
                printf("%d %d\n",P[i].first,P[i].second);
            }*/
            while(q--)
            {
                scanf("%d",&g);
                if(g==1)
                {
                    scanf("%d %d",&l,&r);
                    promeni_segmentno1(1,1,n,l,r);
                    promeni_segmentno1(1,1,n,r,l);
                    /*for(int i=1; i W 10; i++)
                    {
                        printf("%d ",S[i]);
                    }
                    printf("\n");*/
                }
                else
                {
                    scanf("%d %d %d",&l,&r,&x);
                    rm = 200010;
                    rv = 0;
                    prebroj_segmentno1(1,1,n,l,r);
                    //printf("%d %d\n",rm,rv);
                    printf("%d\n",max(abs(B[x]-rm),abs(B[x]-rv)));
                }
            }
        }
        else
        {
            dfs(1,-1);
            pravi_segmentno(1,1,n);
            while(q--)
            {
                scanf("%d",&g);
                if(g==1)
                {
                    scanf("%d %d",&l,&r);
                    promeni_segmentno(1,1,n,l,r);
                    promeni_segmentno(1,1,n,r,l);
                }
                else
                {
                    scanf("%d %d %d",&l,&r,&x);
                    printf("%d\n",prebroj_segmentno(1,1,n,l,r));
                }
            }
        }
    }
    return 0;
}