Why do I get CRASH(ACCESS_VIOLATION)? I don't understand!
program ural1016;
const
top:array[1..6{front},1..6{left}]of byte=
((0,4,2,5,3,0),(3,0,6,1,0,4),(5,1,0,0,6,2),
(2,6,0,0,1,5),(4,0,1,6,0,3),(0,3,5,2,4,0));
var
side:array[1..6]of integer;{top,front,right,bottom,back,left}
dist:array['a'..'h',1..8,1..6{front},1..6{left}]of longint;
q:array[1..10000]of record
x:char;
y,f,l:byte;
pre:integer;
end;
sx,tx,x:char;
sy,ty,y,f,l:byte;
front,rear,p:integer;
min:longint;
route:string;
procedure add(x1:char;y1,f1,l1,cost:byte);
begin
if dist[x,y,f,l]+side[cost]>=dist[x1,y1,f1,l1] then exit;
dist[x1,y1,f1,l1]:=dist[x,y,f,l]+side[cost];
inc(rear);
with q[rear] do begin
x:=x1;y:=y1;f:=f1;l:=l1;pre:=front;
end;
if (x1=tx) and (y1=ty) then
if dist[x1,y1,f1,l1]<min then begin
min:=dist[x1,y1,f1,l1];
p:=rear;
end;
end;
begin
for x:='a' to 'h' do
for y:=1 to 8 do
for f:=1 to 6 do
for l:=1 to 6 do
dist[x,y,f,l]:=maxlongint;
min:=maxlongint;
read(sx);read(x);sy:=ord(x)-48;read(x);
read(tx);read(x);ty:=ord(x)-48;
readln(side[2],side[5],side[1],side[3],side[6],side[4]);
dist[sx,sy,2,4]:=side[6];
front:=0;rear:=1;
q[1].x:=sx;q[1].y:=sy;q[1].f:=2;q[1].l:=4;
repeat
inc(front);
x:=q[front].x;y:=q[front].y;f:=q[front].f;l:=q[front].l;
if y<8 then add(x,succ(y),7-top[f,l],l,7-f);{Go up}
if y>1 then add(x,pred(y),top[f,l],l,f);{Go down}
if x>'a' then add(pred(x),y,f,top[f,l],l);{Go left}
if x<'h' then add(succ(x),y,f,7-top[f,l],7-l);{Go right}
until front=rear;
write(min);
route:=' '+q[p].x+chr(48+q[p].y);
while p>1 do begin
p:=q[p].pre;
route:=' '+q[p].x+chr(48+q[p].y)+route;
end;
writeln(route);
end.