#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
const int Len=105;
typedef struct SNode* Stack;
struct SNode {
int data;
struct SNode* next;
};
typedef struct Node{
int nv;
int ne;
int data[Len][Len];
int visited[Len];
}*graph;
graph CreateGraph(int n)
{
int i ,j;
graph g = (graph)malloc(sizeof(struct Node));
g->nv=n;
g->ne=0;
for (i=0;i<g->nv;++i){
g->visited[i]=0;
for(j=0;j<g->nv;++j){
g->data[i][j]=0;
}
}
return g;
}
void InsertEdge(graph g,int s,int e,int pow)
{
g->data[s][e]=pow;
}
Stack MakeStack()
{
Stack ptrl;
ptrl=(Stack)malloc(sizeof(struct SNode));
ptrl->next=NULL;
return ptrl;
}
int IsEmpty(Stack s)
{
if(s->next==NULL)
return(1);
else
return(0);
}
void push(Stack p, int s)
{
Stack ptrl;
ptrl = (Stack)malloc(sizeof(struct SNode));
ptrl->data = s;
ptrl->next = p->next;
p->next = ptrl;
}
int pop(Stack p)
{
int s;
Stack ptrl;
if (IsEmpty(p)) {
printf("full");
}
else {
ptrl = p->next;
s=ptrl->data;
p->next = ptrl->next;
free(ptrl);
}
return s;
}
void DFS(graph g,int n,Stack s){
g->visited[n]=1;
for (int i=0;i<g->nv;++i){
if(g->data[n][i]!=0&&g->visited[i]!=1){
push(s,i);
}
}
if(!IsEmpty(s)){
int ex=pop(s);
printf("V%d ",ex);
DFS(g,ex,s);
}
else
{
printf("\n");
}
}
int main(){
int n,i,q,b,c,d,m,s,sum=1;
scanf("%d %d",&s,&q);
graph g=CreateGraph(s);
Stack ptrl=MakeStack();
for(i=0;i<q;++i){
scanf("%d %d %d",&b,&c,&d);
InsertEdge(g,b,c,d);
}
while(~scanf("%d",&n),n!=-1){
for (i=0;i<s;++i){
g->visited[i]=0;
}
push(ptrl,n);
printf("DFS From V%d:",n);
printf("V%d ",pop(ptrl));
DFS(g,n,ptrl);
}
system("pause");
return 0;
}