def main():
        N = int(input())
        A = list(map(str, input().split()))
        Q = int(input())
        BC = [list(map(int,input().split())) for i in range(Q)]

        ans = []

        temp_s = A

        for i in range(Q):
                temp_s = [s.replace(str(BC[i][0]),str(BC[i][1])) for s in temp_s]
                temp_i = [int(s) for s in temp_s]
                ans.append(sum(temp_i))

        return ans

if __name__=="__main__":
        ans = main()

        n = len(ans)
        for i in range(n):
                print(ans[i])