def prim(n, e):
from heapq import heappop, heappush
q, vis, ans = [[0, 0]], [0] * n, 0
while q:
du, u = heappop(q)
if vis[u]: continue
vis[u], ans = 1, ans + du
for dv, v in e[u]:
if vis[v]: continue
heappush(q, [dv, v])
return ans