N = int(input())
given_list = [str(i) for i in input().split()]
Q = int(input())

for _ in range(Q):
    query = input().split()
    query = [str(i) for i in query]

    B = query[0]
    C = query[1]
    
    if B in given_list:
        given_list = sorted(given_list)
        for i in range(N):
          if given_list[i] == B:
              given_list[i] = C
          elif int(given_list[i]) > int(C):
              break
        given_list = [int(i) for i in given_list]  
        print(sum(given_list)) #####          
    else:
        given_list = [int(i) for i in given_list]  
        print(sum(given_list)) #####
    
    given_list = [str(i) for i in given_list]