t=int(input())
for i in range(t):
  a=list(map(int,input().strip().split()))
  q=int(input())
  for i in range(q):
    m,n=map(int,input().strip().split())
    for i in range(len(a)):
      if a[i]==m:
        a[i]=n
    print(sum(a))
        