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