#include <bits/stdc++.h>
using namespace std;
long long sum;
int num[5001];
int a[5001];
int main() {
  int n, q, b, c;
  scanf("%d", &n);
  sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
    num[a[i]]++;
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &b, &c);
    sum += (c - b) * num[b];
    num[c] += num[b];
    num[b] = 0;
    printf("%lld\n", sum);
  }
  return 0;
}
