#include <iostream>

long long int arr[100010];

int main(){
    int n;
    long long int sum = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        long long int a;
        scanf("%lld", &a);
        sum += a;
        arr[a]++;
    }
    int q;
    scanf("%d", &q);
    for (int i = 0; i < q; i++)
    {
        long long int b, c;
        scanf("%lld %lld", &b, &c);
        sum += arr[b] * (c - b);
        arr[c] += arr[b];
        arr[b] = 0;
}