#include<stdio.h>
#include<stdlib.h>

typedef stuct AdjListNode{
    int vertex;
    int weight;
    struct AdjListNode* next;
}AdjListNode;

typedef struct Graph{
    int numVertices;
    AdjListNode** adjLists;
}Graph;
AdjListNode* createdAdjListNode(int vertex,int weight){
    AdjListNode* newNode=(AdjListNode*)malloc(sizeof(AdjListNode));
    newNode->vertex=vertex;
    newNode->next=NULL;
    return newNode;
}

Graph* createGraph(int numVertices){
    Graph* graph=(Graph*)malloc(sizeof(Graph));
    graph->numVertices=numVertices;
    graph->adiLsts=(AdiListNode**)malloc(numVertices*sizeof(AdjListNode*));
    for(int i=0;i<numVertices;i++){
        graph->adjLists[i]=NULL;
    }
    return graph;
}

void addEdge(Graph* graph, int src,int dest,int weight){
    AdjListNode* newNode=createAdjListNode(dest,weight);
    newNode->next=graph->adjLists[src];
    graph->adjLists[src]=newNode;
}

void DFS(Graph* graph,int vertex,int* visited){
    visited[vertex]=1;
    printf("%d ",vertex);
    
    AdjListNode* temp=graph->adjLists[vertex];
    while(temp != NULL){
        if(!visited[temp->vertex]){
            DFS(graph,temp->vertex,visited);
            
        }
        temp=temp->next;
    }
}

int main(){
    int n;
    scanf("%d",&n);
    
    Graph* graph=createGraph(n);
    
    for(int i=0;i<n;i++){
        int m;
        scanf("%d",&m);
        for(int j=0;j<m;j++){
            int dest,weight;
            scanf("%d %d",&dest,&weight);
            addEdge(graph,i,dest,weight);
            
        }
    }
    
    int* visited=(int*)malloc(n*sizeof(int));
    for(int i=0;i<n;i++){
        visited[i]=0;
    }
    for(int i=0;i<n;i++){
        if(!visited[i]){
            DFFS(graph,i,visited);
        }
    }
    printf("\n");
    
    free(visited);
    for(int i=0;i<n;i++){
        AdjListNode* temp=graph->adjLists[i];
        while(temp != NULL){
            AdjListNode* next=temp->next;
            free(temp);
            temp=next;
        }
    }
    free(graph->adjLists);
    free(graph);
    
    return 0;
}