#include <bits/stdc++.h>
using namespace std;
int main() {
  map<long long, long long> a;
  for (long long i = 0; i < (long long)(10001); i++) {
    a[i] = 0;
  }
  long long n;
  cin >> n;
  for (long long i = 0; i < (long long)(n); i++) {
    long long temp;
    cin >> temp;
    a[temp]++;
  }
  long long q;
  cin >> q;
  vector<long long> s;
  long long ans;
  for (long long i = 0; i < (long long)(q); i++) {
    long long b, c;
    cin >> b >> c;
    a[c] = a[b] + a[c];
    a[b] = 0;
    ans = 0;
    for (long long j = 0; j < (long long)(10001); j++) {
      long long t;
      t = a[j] * j;
      ans += t;
    }
    s.push_back(ans);
  }
  for (long long i = 0; i < (long long)(q); i++) {
    cout << s.at(i) << endl;
  }
}
