#include <bits/stdc++.h>
using namespace std;
template <class T>
using vec = vector<T>;
using ll = long long;
using vi = vec<int>;
using vvi = vec<vi>;
using vvvi = vec<vvi>;
using pii = pair<int, int>;
template <class T>
pair<T, size_t> max(vector<T> &x) {
  auto it = max_element((x).begin(), (x).end());
  return make_pair(*it, it - x.begin());
}
template <class T>
pair<T, size_t> min(vector<T> &x) {
  auto it = min_element((x).begin(), (x).end());
  return make_pair(*it, it - x.begin());
}
template <class T>
inline bool chmax(T &maxval, const T &newval) {
  if (maxval < newval) {
    maxval = newval;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T &minval, const T &newval) {
  if (minval > newval) {
    minval = newval;
    return 1;
  }
  return 0;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
inline int in() {
  int x;
  cin >> x;
  return x;
}
inline ll IN() {
  ll x;
  cin >> x;
  return x;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  ll ret = 0;
  vi a(n);
  for (int i = int(0); i < int(n); ++i) {
    cin >> a[i];
    ret += a[i];
  }
  unordered_map<int, ll> hist;
  for (int i = int(0); i < int(n); ++i) hist[a[i]]++;
  int q;
  cin >> q;
  int b, c;
  for (int i = int(0); i < int(q); ++i) {
    cin >> b >> c;
    if (hist.find(b) != hist.end()) {
      ret -= hist[b] * b;
      ret += hist[b] * c;
      hist[c] += hist[b];
      hist[b] = 0;
    }
  }
  return 0;
}
