la = list(map(int,input().split()))
q = int(input())
lb = [0]*q
lc = [0]*q
ld = [0]*q
for i in range(q):
  lb[i],lc[i] = map(int,input().split())
  ld[i] = lc[i] - lb[i]
s = sum(la)
for i in range(q):
  m = la.count(lb[i])
  s = s + ld[i]*m
  print(s)
  if m!=0:
    la.remove(lb[i])
    for j in range(m):
      la.append(lc[i])