Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def prim_heap():
    used = [True] * n #True:不使用
    edgelist = []
    for e in edge[0]:
        heapq.heappush(edgelist,e)
    used[0] = False
    res = 0
    while len(edgelist) != 0:
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,e)
        res += minedge[0]
    return res

#########################
n= int(input())

edge = [[] for i in range(n)]
#隣接リスト edge[i]:[コスト,行先]
l=[]
for i in range(n):
    a,b=map(int,input().split())
    l.append([a,b,i])
l=sorted(l,key=lambda x: -x[0])
for i in range(n-1):
    x=l[i][2]
    y=l[i+1][2]
    z=l[i][0]-l[i+1][0]
    edge[x].append([z,y])
    edge[y].append([z,x])
l=sorted(l,key=lambda x: -x[1])
for i in range(n-1):
    x=l[i][2]
    y=l[i+1][2]
    z=l[i][1]-l[i+1][1]
    edge[x].append([z,y])
    edge[y].append([z,x])
print(prim_heap())


Fixed submission:
import heapq
import sys
input = sys.stdin.readline
def prim_heap():
    used = [True] * n #True:不使用
    edgelist = []
    for e in edge[0]:
        heapq.heappush(edgelist,e)
    used[0] = False
    res = 0
    while len(edgelist) != 0:
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,e)
        res += minedge[0]
    return res

#########################
n= int(input())

edge = [[] for i in range(n)]
#隣接リスト edge[i]:[コスト,行先]
l=[]
for i in range(n):
    a,b=map(int,input().split())
    l.append([a,b,i])
l=sorted(l,key=lambda x: -x[0])
for i in range(n-1):
    x=l[i][2]
    y=l[i+1][2]
    z=l[i][0]-l[i+1][0]
    edge[x].append([z,y])
    edge[y].append([z,x])
l=sorted(l,key=lambda x: -x[1])
for i in range(n-1):
    x=l[i][2]
    y=l[i+1][2]
    z=l[i][1]-l[i+1][1]
    edge[x].append([z,y])
    edge[y].append([z,x])
print(prim_heap())

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def prim_heap():
    used = [True] * n #True:不使用
    edgelist = []
    for e in edge[0]:
        heapq.heappush(edgelist,e)
    used[0] = False
    res = 0
    while len(edgelist) != 0:
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,e)
        res += minedge[0]
    return res

#########################
n= int(input())

edge = [[] for i in range(n)]
#隣接リスト edge[i]:[コスト,行先]
l=[]
for i in range(n):
    a,b=map(int,input().split())
    l.append([a,b,i])
l=sorted(l,key=lambda x: -x[0])
for i in range(n-1):
    x=l[i][2]
    y=l[i+1][2]
    z=l[i][0]-l[i+1][0]
    edge[x].append([z,y])
    edge[y].append([z,x])
l=sorted(l,key=lambda x: -x[1])
for i in range(n-1):
    x=l[i][2]
    y=l[i+1][2]
    z=l[i][1]-l[i+1][1]
    edge[x].append([z,y])
    edge[y].append([z,x])
print(prim_heap())

Fixed submission:
import heapq
import sys
input = sys.stdin.readline
def prim_heap():
    used = [True] * n #True:不使用
    edgelist = []
    for e in edge[0]:
        heapq.heappush(edgelist,e)
    used[0] = False
    res = 0
    while len(edgelist) != 0:
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,e)
        res += minedge[0]
    return res

#########################
n= int(input())

edge = [[] for i in range(n)]
#隣接リスト edge[i]:[コスト,行先]
l=[]
for i in range(n):
    a,b=map(int,input().split())
    l.append([a,b,i])
l=sorted(l,key=lambda x: -x[0])
for i in range(n-1):
    x=l[i][2]
    y=l[i+1][2]
    z=l[i][0]-l[i+1][0]
    edge[x].append([z,y])
    edge[y].append([z,x])
l=sorted(l,key=lambda x: -x[1])
for i in range(n-1):
    x=l[i][2]
    y=l[i+1][2]
    z=l[i][1]-l[i+1][1]
    edge[x].append([z,y])
    edge[y].append([z,x])
print(prim_heap())