#include <bits/stdc++.h>
int a[500001], n, q, x, s;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &x), a[x]++, s += x;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int y;
    scanf("%d%d", &x, &y);
    s -= x * a[x] - y * a[x], a[y] += a[x], a[x] = 0;
    printf("%d\n", s);
  }
}
