#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cassert>

std::vector<int> slow(int n, std::vector<std::pair<int, char>> input) {
    std::vector<char> cost(1+n);
    std::vector<int> answ(1+n);
    std::vector<std::vector<int>> edges(1+n);
    // Edges from input vector:
    for (int u = 2; u <= n; ++u) {
        int p = input[u].first; 
        cost[u] = input[u].second;
        edges[p].push_back(u);
        edges[u].push_back(p);
    }
    // DFS function:
    std::function<std::string(int,int)> visit = [&](int u, int p) {
        std::string best; int r = u;
        for (int v : edges[u]) {
            if (v == p) continue;
            auto temp = cost[v] + visit(v, u); // get string from child
            if (temp.size() > best.size()) { // comparison this string with best string at this moment
                best = temp;
                r = v;
            } else if (temp.size() == best.size()) {
                if (temp > best || (temp == best && answ[v] < answ[r])) {
                    best = temp;
                    r = v;
                }
            }
        }
        answ[u] = (r == u) ? r : answ[r];
        return best;
    };
    visit(1,0); // run dfs from root
    // Return answer
    for (int u = 1; u <= n; ++u) {
        if (answ[u] == u) answ[u] = 0;
    }
    return answ;
}

std::vector<std::pair<int, char>> input(int& n) {
    scanf("%d", &n);
    std::vector<std::pair<int, char>> answ(1+n);
    for (int u = 2; u <= n; ++u) {
        int p; char c;
        scanf("%d %c", &p, &c);
        answ[u] = std::make_pair(p,c);
    }
    return answ;
}

int main() {
    int n;
    auto in = input(n);
    auto answ = slow(n, in);
    for (int u = 1; u <= n; ++u) {
        printf("%d\n", answ[u]);
    }
    return 0;
}