#include<stdio.h>
#include<assert.h>
#define min(a,b) (a<b)?a:b
int heapsize=0,t[251][251],mindis[251],seen[251],s,g,d,m,n;
typedef struct node{
int vertex,length;
}node;
node heap[63000];
void insert(node x)
{
heapsize++;
heap[heapsize]=x;
int now=heapsize,parent=heapsize/2;
while(heap[parent].length>x.length&&parent>=1)
{
heap[now]=heap[parent];
now/=2,parent/=2;
}
heap[now]=x;
}
void delete_min()
{
node temp=heap[heapsize--];
heap[1]=temp;
int now=1,leftchild=2,rightchild=3;
while(1)
{
leftchild=2*now,rightchild=2*now+1;
if(leftchild>heapsize)
break;
if(heap[now].length>heap[leftchild].length)
{
heap[now]=heap[leftchild];
heap[leftchild]=temp;
now=leftchild;
continue;
}
if(rightchild>heapsize)
{
break;
}
if(heap[leftchild].length>=heap[now].length&&heap[rightchild].length>=heap[now].length)
{
break;
}
if(heap[now].length>heap[rightchild].length)
{
heap[now]=heap[rightchild];
heap[rightchild]=temp;
now=rightchild;
continue;
}
}
}
void shortest(int st)
{
int i,presentvertex;
heapsize=0;
node temp,min;
for( i=1;i<=n;i++)
{
if(i==st)
{
mindis[i]=0;
seen[i]=1;
}
else
{
mindis[i]=t[st][i];
temp.vertex=i;
temp.length=t[st][i];
insert(temp);
seen[i]=0;
}
}
while(heapsize>0)
{
min=heap[1];
delete_min();
//for(i=1;i<=heapsize;i++)
//printf("Vertex:%d Distance:%d ",heap[i].vertex,heap[i].length);
//printf("\n");
presentvertex=min.vertex;
if(seen[presentvertex])
{
continue;
}
seen[presentvertex]=1;
for(i=1;i<=heapsize;i++)
{
if(seen[i])
continue;
if(mindis[i]>t[presentvertex][i]+mindis[presentvertex])
{
mindis[i]=t[presentvertex][i]+mindis[presentvertex];
temp.vertex=i;
temp.length=mindis[i];
insert(temp);
}
}
}
}
int main()
{
int i,j,a,b,c;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&t[i][j]);
}
}
scanf("%d",&m);
while(m--)
{
scanf("%d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