N = int(eval(input()))

Cs = list(map(int, input().split()))

adj = [[] for _ in range(N)]

for _ in range(N - 1):

    a, b = list(map(int, input().split()))

    adj[a - 1].append(b - 1)

    adj[b - 1].append(a - 1)



degs = [0] * N

clusters = [[] for _ in range(N)]





def go(curr, fro):

    col = Cs[curr] - 1

    deg = 1

    clust = {}

    for n in adj[curr]:

        if n == fro:

            continue

        ch_deg, ch_clust = yield go(n, curr)

        clusters[col].append(ch_deg - ch_clust.get(col, 0))

        if len(ch_clust) > len(clust):

            clust, ch_clust = ch_clust, clust

        for k, v in list(ch_clust.items()):

            clust[k] = clust.get(k, 0) + v

        deg += ch_deg

    clust[col] = deg

    return deg, clust





def tramp(gen):

    stack = [gen]

    val = None

    while stack:

        try:

            stack.append(stack[-1].send(val))

            val = None

        except StopIteration as e:

            stack.pop()

            val = e.value

    return val





_, root_clust = tramp(go(0, -1))

for i in range(N):

    clusters[i].append(N - root_clust.get(i, 0))





for clust in clusters:

    tot = (N * N + N) // 2

    for cnt in clust:

        tot -= (cnt * cnt + cnt) // 2

    print(tot)

N = int(eval(input()))

Cs = list(map(int, input().split()))

adj = [[] for _ in range(N)]

for _ in range(N - 1):

    a, b = list(map(int, input().split()))

    adj[a - 1].append(b - 1)

    adj[b - 1].append(a - 1)



under_col = [0] * N

ans = [0] * N





def tri(n):

    return n * (n + 1) // 2





def go(curr, fro):

    col = Cs[curr] - 1

    prev_under = under_col[col]

    deg = 1

    for n in adj[curr]:

        if n == fro:

            continue

        curr_under = under_col[col]

        ch_deg = yield go(n, curr)

        diff_under = under_col[col] - curr_under

        ans[col] += tri(ch_deg - diff_under)

        deg += ch_deg

    under_col[col] = prev_under + deg

    return deg





def tramp(gen):

    stack = [gen]

    val = None

    while stack:

        try:

            stack.append(stack[-1].send(val))

            val = None

        except StopIteration as e:

            stack.pop()

            val = e.value

    return val





tramp(go(0, -1))

for i in range(N):

    ans[i] += tri(N - under_col[i])

    print((tri(N) - ans[i]))

N = int(eval(input()))
Cs = list(map(int, input().split()))
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = list(map(int, input().split()))
    adj[a - 1].append(b - 1)
    adj[b - 1].append(a - 1)
degs = [0] * N
clusters = [[] for _ in range(N)]


def go(curr, fro):
    col = Cs[curr] - 1
    deg = 1
    clust = {}
    for n in adj[curr]:
        if n == fro:
            continue
        ch_deg, ch_clust = yield go(n, curr)
        clusters[col].append(ch_deg - ch_clust.get(col, 0))
        if len(ch_clust) > len(clust):
            clust, ch_clust = ch_clust, clust
        for k, v in list(ch_clust.items()):
            clust[k] = clust.get(k, 0) + v
        deg += ch_deg
    clust[col] = deg
    return deg, clust


def tramp(gen):
    stack = [gen]
    val = None
    while stack:
        try:
            stack.append(stack[-1].send(val))
            val = None
        except StopIteration as e:
            stack.pop()
            val = e.value
    return val


_, root_clust = tramp(go(0, -1))
for i in range(N):
    clusters[i].append(N - root_clust.get(i, 0))
for clust in clusters:
    tot = (N * N + N) // 2
    for cnt in clust:
        tot -= (cnt * cnt + cnt) // 2
    print(tot)

N = int(eval(input()))
Cs = list(map(int, input().split()))
adj = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = list(map(int, input().split()))
    adj[a - 1].append(b - 1)
    adj[b - 1].append(a - 1)
under_col = [0] * N
ans = [0] * N


def tri(n):
    return n * (n + 1) // 2


def go(curr, fro):
    col = Cs[curr] - 1
    prev_under = under_col[col]
    deg = 1
    for n in adj[curr]:
        if n == fro:
            continue
        curr_under = under_col[col]
        ch_deg = yield go(n, curr)
        diff_under = under_col[col] - curr_under
        ans[col] += tri(ch_deg - diff_under)
        deg += ch_deg
    under_col[col] = prev_under + deg
    return deg


def tramp(gen):
    stack = [gen]
    val = None
    while stack:
        try:
            stack.append(stack[-1].send(val))
            val = None
        except StopIteration as e:
            stack.pop()
            val = e.value
    return val


tramp(go(0, -1))
for i in range(N):
    ans[i] += tri(N - under_col[i])
    print((tri(N) - ans[i]))

-degs = [0] * N -clusters = [[] for _ in range(N)] +under_col = [0] * N +ans = [0] * N + + +def tri(n): +    return n * (n + 1) // 2 +    prev_under = under_col[col] -    clust = {} -        ch_deg, ch_clust = yield go(n, curr) -        clusters[col].append(ch_deg - ch_clust.get(col, 0)) -        if len(ch_clust) > len(clust): -            clust, ch_clust = ch_clust, clust -        for k, v in list(ch_clust.items()): -            clust[k] = clust.get(k, 0) + v +        curr_under = under_col[col] +        ch_deg = yield go(n, curr) +        diff_under = under_col[col] - curr_under +        ans[col] += tri(ch_deg - diff_under) -    clust[col] = deg -    return deg, clust +    under_col[col] = prev_under + deg +    return deg -_, root_clust = tramp(go(0, -1)) +tramp(go(0, -1)) -    clusters[i].append(N - root_clust.get(i, 0)) -for clust in clusters: -    tot = (N * N + N) // 2 -    for cnt in clust: -        tot -= (cnt * cnt + cnt) // 2 -    print(tot) +    ans[i] += tri(N - under_col[i]) +    print((tri(N) - ans[i]))