#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 5LL << 60;
const ll mod = 1e9 + 7;

int main(void) {
	int N;
	cin >> N;
	int A[N], B[N], C[N];
	for(int i = 0; i < N; i++) cin >> A[i];
	for(int i = 0; i < N; i++) cin >> B[i];
	for(int i = 0; i < N; i++) {
		cin >> C[i];
		C[i]--;
	}

	int cnt[100010];
	for(int i = 0; i < 100010; i++) cnt[i] = 0;
	for(int i = 0; i < N; i++) cnt[A[i]]++;

	ll ans = 0;
	for(int i = 0; i < N; i++) {
		ans += cnt[B[C[i]]];
	}

	cout << ans << "\n";
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define fora(i, x, n) for(int i=(x); i<(n) ; ++i)
#define trav(a, A) for(auto& a : A)
#define endl '\n'
#define ford(i, x) for(int i=(x); i>=0 ; --i)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define INF ((long long int) ((unsigned long long int)~0>>1))

using li = long long int;
using vi = vector<int>;

unordered_map<int,int> freq_colors;
vi color;
vector<vi> tree;
vi good_vertex;

void dfs(int u, int parent = 0){

	freq_colors[color[u]]++;
	if ( freq_colors[color[u]] == 1 ){
		good_vertex.pb(u);
	}
	for(auto v : tree[u]){
		if (v == parent)	continue;
		
		dfs(v, u);
	}
	freq_colors[color[u]]--;
}

void solve(){
	int n;
	cin >>n;
	
	color.resize(n);
	tree.resize(n);
	
	for(auto& c : color){
		cin >> c;
	}
	int a, b;
	fora(i, 0, n-1){
		cin >> a >> b; a--; b--;
		tree[a].pb(b);
		tree[b].pb(a);
	}
	
	dfs(0, 0);
	sort(all(good_vertex));
	
	for(auto& v : good_vertex){
		cout << v+1 << endl;
	}
}

int main(void){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
	solve();
    return 0;
}
