import sys

input = sys.stdin.readline
# fin = open('input.txt', 'r')
# input = fin.readline

down_nodes = []
up_index = 0
down_index = 0


def make_number(edge):
    global down_nodes
    global down_index
    global up_index

    e = edge[1]
    if e in down_nodes:
        down_index += 1
        return down_index - 1
    up_index += 1
    return up_index - 1


def solve():
    global down_nodes
    global up_index

    input()
    lines = sys.stdin.readlines()
    edges = [tuple(map(int, line.split())) for line in lines]
    a = list(zip(*edges))
    up_nodes = set(a[0])
    down_nodes = [node for node in list(a[1]) if node not in up_nodes]
    start_up_count = len(down_nodes)
    up_index = start_up_count
    ans = '\n'.join([str(make_number(edge)) for edge in edges])
    print(ans)


solve()
