Buggy python submission (verdict: Runtime Error):
import math

def routing(s,g,route):
    ans=[]
    i=g
    while i!=-1:
        ans.append(i+1)
        i=route[i]
    return ans[::-1]

inf=100000
while True:
    n=input()
    if n==0:break
    L=[[inf]*n for i in range(n)]
    for i in range(n):
        L[i][i]=0
    B=[map(int,raw_input().split()) for i in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            ba,xa,ya=B[i]
            bb,xb,yb=B[j]
            ba,bb=ba-1,bb-1
            d=math.sqrt(pow(xa-xb,2)+pow(ya-yb,2))
            if d<=50:
                L[ba][bb]=L[bb][ba]=d

    m=input()
    for p in range(m):
        s,g=map(int,raw_input().split())
        s,g=s-1,g-1
        size=len(L)
        done=[s]
        dist=[inf]*size
        dist[s]=0
        route=[-1]*size
        i=s
        while len(done)<size:
            for j in range(size):
                if j in done:
                    continue
                if dist[j]>dist[i]+L[i][j]:
                    dist[j]=dist[i]+L[i][j]
                    route[j]=i
            min_node_size=inf
            for v,w in enumerate(dist):
                if v not in done and w<min_node_size:
                    min_node_size=w
                    min_node_num=v
            i=min_node_num
            done.append(min_node_num)
        if route[g]==-1:
            print 'NA'
        else:
            ans=routing(s,g,route)
            print " ".join(map(str,ans))

Fixed submission:
import math

def dijkstra(s):
    d=[inf]*n
    pi=[inf]*n
    visited=[False]*n
    d[s]=0
    pi[s]=-1
    while True:
        mincost=inf
        for i in range(n):
            if not visited[i] and d[i]<mincost:
                mincost=d[i]
                u=i
        if mincost==inf:
            break
        visited[u]=True
        for v in range(n):
            if visited[v] or L[u][v]==inf:
                continue
            if d[u]+L[u][v]<d[v]:
                d[v]=d[u]+L[u][v]
                pi[v]=u
    return pi

def routing(pi,g):
    ans=[]
    i=g
    while i!=-1:
        ans.append(i+1)
        i=pi[i]
    return ans[::-1]

inf=100000
while True:
    n=input()
    if n==0:break
    L=[[inf]*n for i in range(n)]
    for i in range(n):
        L[i][i]=0
    B=[map(int,raw_input().split()) for i in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            ba,xa,ya=B[i]
            bb,xb,yb=B[j]
            ba,bb=ba-1,bb-1
            d=math.sqrt(pow(xa-xb,2)+pow(ya-yb,2))
            if d<=50:
                L[ba][bb]=L[bb][ba]=d
    m=input()
    for i in range(m):
        s,g=map(int,raw_input().split())
        s,g=s-1,g-1
        pi=dijkstra(s)
        if pi[g]==inf:
            print "NA"
        else:
            ans=routing(pi,g)
            print " ".join(map(str,ans))

Buggy python submission (verdict: Runtime Error):
import math

def routing(s,g,route):
    ans=[]
    i=g
    while i!=-1:
        ans.append(i+1)
        i=route[i]
    return ans[::-1]

inf=100000
while True:
    n=input()
    if n==0:break
    L=[[inf]*n for i in range(n)]
    for i in range(n):
        L[i][i]=0
    B=[map(int,raw_input().split()) for i in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            ba,xa,ya=B[i]
            bb,xb,yb=B[j]
            ba,bb=ba-1,bb-1
            d=math.sqrt(pow(xa-xb,2)+pow(ya-yb,2))
            if d<=50:
                L[ba][bb]=L[bb][ba]=d

    m=input()
    for p in range(m):
        s,g=map(int,raw_input().split())
        s,g=s-1,g-1
        n=len(L)
        done=[s]
        dist=[inf]*n
        dist[s]=0
        route=[-1]*n
        i=s
        while len(done)<n:
            for j in range(n):
                if j in done:
                    continue
                if dist[j]>dist[i]+L[i][j]:
                    dist[j]=dist[i]+L[i][j]
                    route[j]=i
            min_node_size=inf
            for v,w in enumerate(dist):
                if v not in done and w<min_node_size:
                    min_node_size=w
                    min_node_num=v
            i=min_node_num
            done.append(min_node_num)
        if dist[g]==inf:
            print 'NA'
        else:
            ans=routing(s,g,route)
            print " ".join(map(str,ans))

Fixed submission:
import math

def dijkstra(s):
    d=[inf]*n
    pi=[inf]*n
    visited=[False]*n
    d[s]=0
    pi[s]=-1
    while True:
        mincost=inf
        for i in range(n):
            if not visited[i] and d[i]<mincost:
                mincost=d[i]
                u=i
        if mincost==inf:
            break
        visited[u]=True
        for v in range(n):
            if visited[v] or L[u][v]==inf:
                continue
            if d[u]+L[u][v]<d[v]:
                d[v]=d[u]+L[u][v]
                pi[v]=u
    return pi

def routing(pi,g):
    ans=[]
    i=g
    while i!=-1:
        ans.append(i+1)
        i=pi[i]
    return ans[::-1]

inf=100000
while True:
    n=input()
    if n==0:break
    L=[[inf]*n for i in range(n)]
    for i in range(n):
        L[i][i]=0
    B=[map(int,raw_input().split()) for i in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            ba,xa,ya=B[i]
            bb,xb,yb=B[j]
            ba,bb=ba-1,bb-1
            d=math.sqrt(pow(xa-xb,2)+pow(ya-yb,2))
            if d<=50:
                L[ba][bb]=L[bb][ba]=d
    m=input()
    for i in range(m):
        s,g=map(int,raw_input().split())
        s,g=s-1,g-1
        pi=dijkstra(s)
        if pi[g]==inf:
            print "NA"
        else:
            ans=routing(pi,g)
            print " ".join(map(str,ans))

Buggy python submission (verdict: Runtime Error):
import math

def routing(s,g,route):
    ans=[]
    i=g
    while i!=-1:
        ans.append(i+1)
        i=route[i]
    return ans[::-1]

inf=100000
while True:
    n=input()
    if n==0:break
    L=[[inf]*n for i in range(n)]
    for i in range(n):
        L[i][i]=0
    B=[map(int,raw_input().split()) for i in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            ba,xa,ya=B[i]
            bb,xb,yb=B[j]
            ba,bb=ba-1,bb-1
            d=math.sqrt(pow(xa-xb,2)+pow(ya-yb,2))
            if d<=50:
                L[ba][bb]=L[bb][ba]=d

    m=input()
    for p in range(m):
        s,g=map(int,raw_input().split())
        s,g=s-1,g-1
        n=len(L)
        done=[s]
        dist=[inf]*n
        dist[s]=0
        route=[-1]*n
        i=s
        while len(done)<n:
            for j in range(n):
                if j in done:
                    continue
                if dist[j]>dist[i]+L[i][j]:
                    dist[j]=dist[i]+L[i][j]
                    route[j]=i
            min_node_size=inf
            for v,w in enumerate(dist):
                if v not in done and w<min_node_size:
                    min_node_size=w
                    min_node_num=v
            i=min_node_num
            done.append(min_node_num)
        print 'NA'

Fixed submission:
import math

def dijkstra(s):
    d=[inf]*n
    pi=[inf]*n
    visited=[False]*n
    d[s]=0
    pi[s]=-1
    while True:
        mincost=inf
        for i in range(n):
            if not visited[i] and d[i]<mincost:
                mincost=d[i]
                u=i
        if mincost==inf:
            break
        visited[u]=True
        for v in range(n):
            if visited[v] or L[u][v]==inf:
                continue
            if d[u]+L[u][v]<d[v]:
                d[v]=d[u]+L[u][v]
                pi[v]=u
    return pi

def routing(pi,g):
    ans=[]
    i=g
    while i!=-1:
        ans.append(i+1)
        i=pi[i]
    return ans[::-1]

inf=100000
while True:
    n=input()
    if n==0:break
    L=[[inf]*n for i in range(n)]
    for i in range(n):
        L[i][i]=0
    B=[map(int,raw_input().split()) for i in range(n)]
    for i in range(n-1):
        for j in range(i+1,n):
            ba,xa,ya=B[i]
            bb,xb,yb=B[j]
            ba,bb=ba-1,bb-1
            d=math.sqrt(pow(xa-xb,2)+pow(ya-yb,2))
            if d<=50:
                L[ba][bb]=L[bb][ba]=d
    m=input()
    for i in range(m):
        s,g=map(int,raw_input().split())
        s,g=s-1,g-1
        pi=dijkstra(s)
        if pi[g]==inf:
            print "NA"
        else:
            ans=routing(pi,g)
            print " ".join(map(str,ans))