#!/usr/bin/env python3
n = int(input()) 
A = list(map(str, input().split()))
q = int(input()) 
for i in range(q):
    b, c = map(str, input().split())
    A = [int(str(a).replace(b, c)) for a in A]
    print(sum(A))