Buggy python submission (verdict: Time Limit Exceeded):
import queue

N = int(input())

pos = []
for _ in range(N):
    a, b = map(int, input().split())
    pos.append((a, b))

visited = {v: False for v in pos}
edges = {v: [] for v in pos}
x_sorted = sorted([(p[0], i) for i, p in enumerate(pos)])
for i in range(len(x_sorted) - 1):
    j = x_sorted[i][1]
    l = x_sorted[i+1][1]
    edges[pos[j]].append((pos[l], abs(pos[l][0] - pos[j][0])))
    edges[pos[l]].append((pos[j], abs(pos[l][0] - pos[j][0])))
y_sorted = sorted([(p[1], i) for i, p in enumerate(pos)])
for i in range(len(y_sorted) - 1):
    j = y_sorted[i][1]
    l = y_sorted[i+1][1]
    edges[pos[j]].append((pos[l], abs(pos[l][1] - pos[j][1])))
    edges[pos[l]].append((pos[j], abs(pos[l][1] - pos[j][1])))

que = queue.PriorityQueue()
que.put((0, pos[0]))

res = 0
while not que.empty():
    cost, vertex = que.get()
    if visited[vertex]:
        continue
    visited[vertex] = True
    res += cost

    for to, next_cost in edges[vertex]:
        que.put((next_cost, to))

print(res)


Fixed submission:
import queue

N = int(input())

pos = []
for _ in range(N):
    a, b = map(int, input().split())
    pos.append((a, b))

visited = {v: False for v in pos}
edges = {v: [] for v in pos}
x_sorted = sorted([(p[0], i) for i, p in enumerate(pos)])
for i in range(len(x_sorted) - 1):
    j = x_sorted[i][1]
    l = x_sorted[i+1][1]
    edges[pos[j]].append((pos[l], abs(pos[l][0] - pos[j][0])))
    edges[pos[l]].append((pos[j], abs(pos[l][0] - pos[j][0])))
y_sorted = sorted([(p[1], i) for i, p in enumerate(pos)])
for i in range(len(y_sorted) - 1):
    j = y_sorted[i][1]
    l = y_sorted[i+1][1]
    edges[pos[j]].append((pos[l], abs(pos[l][1] - pos[j][1])))
    edges[pos[l]].append((pos[j], abs(pos[l][1] - pos[j][1])))

que = queue.PriorityQueue()
que.put((0, pos[0]))

res = 0
while not que.empty():
    cost, vertex = que.get()
    if visited[vertex]:
        continue
    visited[vertex] = True
    res += cost

    for to, next_cost in edges[vertex]:
        if not visited[to]:
            que.put((next_cost, to))

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
import queue

N = int(input())

pos = []
for _ in range(N):
    a, b = map(int, input().split())
    pos.append((a - 1, b - 1))

visited = {v: False for v in pos}
que = queue.PriorityQueue()
que.put((0, pos[0]))

res = 0
while not que.empty():
    cost, vertex = que.get()
    if visited[vertex]:
        continue
    visited[vertex] = True
    res += cost

    for i in range(N):
        next_cost = min(abs(pos[i][0] - vertex[0]), abs(pos[i][1] - vertex[1]))
        que.put((next_cost, (pos[i][0], pos[i][1])))

print(res)


Fixed submission:
import queue

N = int(input())

pos = []
for _ in range(N):
    a, b = map(int, input().split())
    pos.append((a, b))

visited = {v: False for v in pos}
edges = {v: [] for v in pos}
x_sorted = sorted([(p[0], i) for i, p in enumerate(pos)])
for i in range(len(x_sorted) - 1):
    j = x_sorted[i][1]
    l = x_sorted[i+1][1]
    edges[pos[j]].append((pos[l], abs(pos[l][0] - pos[j][0])))
    edges[pos[l]].append((pos[j], abs(pos[l][0] - pos[j][0])))
y_sorted = sorted([(p[1], i) for i, p in enumerate(pos)])
for i in range(len(y_sorted) - 1):
    j = y_sorted[i][1]
    l = y_sorted[i+1][1]
    edges[pos[j]].append((pos[l], abs(pos[l][1] - pos[j][1])))
    edges[pos[l]].append((pos[j], abs(pos[l][1] - pos[j][1])))

que = queue.PriorityQueue()
que.put((0, pos[0]))

res = 0
while not que.empty():
    cost, vertex = que.get()
    if visited[vertex]:
        continue
    visited[vertex] = True
    res += cost

    for to, next_cost in edges[vertex]:
        if not visited[to]:
            que.put((next_cost, to))

print(res)