#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  map<int, int> mii;
  int tmp;
  for (int i = 0; i < (N); ++i) {
    cin >> tmp;
    mii[tmp]++;
  }
  map<int, vector<int>> mivi;
  auto s_e = mii.end();
  for (auto itr = mii.begin(); itr != s_e; ++itr) {
    int k = itr->first;
    mivi[k].push_back(k);
  }
  int Q;
  cin >> Q;
  int tmp1, tmp2;
  for (int i = 0; i < (Q); ++i) {
    cin >> tmp1 >> tmp2;
    auto& m1 = mivi[tmp1];
    auto& m2 = mivi[tmp2];
    m2.insert(m2.end(), m1.begin(), m1.end());
    m1.clear();
    map<int, int> mii2;
    auto e1 = mivi.end();
    for (auto itr = mivi.begin(); itr != e1; ++itr) {
      int f = itr->first;
      auto e2 = itr->second.end();
      for (auto itr2 = itr->second.begin(); itr2 != e2; ++itr2) {
        mii2[*itr2] = f;
      }
    }
    long long ans = 0;
    auto end = mii.end();
    for (auto itr = mii.begin(); itr != end; ++itr) {
      int f = itr->first;
      int s = itr->second;
      ans += mii2[f] * (long long)s;
    }
    cout << ans << "\n";
  }
  return 0;
}
