Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3
# -*- coding:utf-8 -*-

from collections import defaultdict
import sys
sys.setrecursionlimit(10**9)

def main():
  n = int(input())
  tree = defaultdict(lambda : {'parent':-1, 'score':0, 'children':[]})
  weights = dict()
  for _ in range(n-1):
    a, b, c = map(int, input().split())
    tree[a]['children'].append(b)
    tree[b]['children'].append(a)
    weights[(a, b)] = weights[(b, a)] = c
    
  q, k = map(int, input().split())  
  visited = [False] * (n+1)
  iroot = k
  def dfs(inode, score=0):
    visited[inode] = True
    for ichild in tree[inode]['children']:
      if visited[ichild]:
        continue
      tree[ichild]['score'] = weights[(inode, ichild)] + score
      tree[ichild]['parent'] = inode
      dfs(ichild, tree[ichild]['score'])
  dfs(iroot)
  for _ in range(q):
    x, y = map(int, input().split())
    print(tree[x]['score'] + tree[y]['score'])

if __name__=='__main__':
  main()



Fixed submission:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import sys
sys.setrecursionlimit(10**9)

def main():
  n = int(input())
  inode2scores = [0] * (n+1)
  tree = [[] for _ in range(n+1)]
  weights = dict()
  for _ in range(n-1):
    a, b, c = map(int, input().split())
    tree[a].append(b)
    tree[b].append(a)
    weights[(a, b)] = weights[(b, a)] = c
    
  q, k = map(int, input().split())  
  visited = [False] * (n+1)
  iroot = k
  def dfs(inode, score=0):
    visited[inode] = True
    for ichild in tree[inode]:
      if visited[ichild]:
        continue
      inode2scores[ichild] = weights[(inode, ichild)] + score
      dfs(ichild, inode2scores[ichild])
  dfs(iroot)
  for _ in range(q):
    x, y = map(int, input().split())
    print(inode2scores[x] + inode2scores[y])

if __name__=='__main__':
  main()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/python3
# -*- coding:utf-8 -*-

from collections import defaultdict

def main():
  n = int(input())
  tree = defaultdict(lambda : {'parent':-1, 'score':0, 'children':[]})
  weights = dict()
  for _ in range(n-1):
    a, b, c = map(int, input().split())
    tree[a]['children'].append(b)
    tree[b]['children'].append(a)
    weights[(a, b)] = weights[(b, a)] = c
    
  q, k = map(int, input().split())  
  visited = []
  iroot = k
  def dfs(inode, score=0):
    visited.append(inode)
    for ichild in tree[inode]['children']:
      if ichild in visited:
        continue
      tree[ichild]['score'] = weights[(inode, ichild)] + score
      tree[ichild]['parent'] = inode
      dfs(ichild, tree[ichild]['score'])
  dfs(iroot)
  for _ in range(q):
    x, y = map(int, input().split())
    print(tree[x]['score'] + tree[y]['score'])

if __name__=='__main__':
  main()



Fixed submission:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import sys
sys.setrecursionlimit(10**9)

def main():
  n = int(input())
  inode2scores = [0] * (n+1)
  tree = [[] for _ in range(n+1)]
  weights = dict()
  for _ in range(n-1):
    a, b, c = map(int, input().split())
    tree[a].append(b)
    tree[b].append(a)
    weights[(a, b)] = weights[(b, a)] = c
    
  q, k = map(int, input().split())  
  visited = [False] * (n+1)
  iroot = k
  def dfs(inode, score=0):
    visited[inode] = True
    for ichild in tree[inode]:
      if visited[ichild]:
        continue
      inode2scores[ichild] = weights[(inode, ichild)] + score
      dfs(ichild, inode2scores[ichild])
  dfs(iroot)
  for _ in range(q):
    x, y = map(int, input().split())
    print(inode2scores[x] + inode2scores[y])

if __name__=='__main__':
  main()