import sys

def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]

def main():
    input = sys.stdin.readline
    n = int(input())
    a = list(map(str, input().strip().split()))
    q = int(input())
    bc = [map(str, input().split()) for _ in range(q)]
    b, c = [list(i) for i in zip(*bc)]
    ii = 0

    for i in b:
        a = [n.replace(i,c[ii]) for n in a]
        print(sum(list(map(int, a))))
        ii += 1
        

if __name__ == "__main__":
    main()