def main():
    n = int(input())
    alis = sorted(map(int, input().split()))
    adic = {}
    for num in alis:
        if num in adic:
            adic[num] += 1
        else:
            adic[num] = 1
    
    anslis = []

    q = int(input())
    bclis = []
    for i in range(q):
        b, c = map(int,input().split())
        if b in adic:
            kazu = adic[b]
            adic[b] = 0
            if c in adic:
                adic[c] += kazu
            else:
                adic[c] = kazu
        ans = 0
        for key in adic:
            ans += adic[key] * key
        print(ans)
    
    
if __name__ == "__main__":
    main()
