#include <bits/stdc++.h>
using namespace std;
string replace_c(string str, string t, string w) {
  if (!t.empty()) {
    size_t pos = 0;
    while ((pos = str.find(t, pos)) != std::string::npos) {
      str.replace(pos, t.length(), w);
      pos += w.length();
    }
  }
  return str;
}
int main() {
  int n;
  cin >> n;
  string s;
  for (int i = 0; i < (int)(n); i++) {
    int a;
    cin >> a;
    s += to_string(a);
    s += ' ';
  }
  int k;
  cin >> k;
  vector<int> b(k);
  vector<int> c(k);
  for (int i = 0; i < (int)(k); i++) {
    cin >> b[i] >> c[i];
  }
  for (int i = 0; i < (int)(k); i++) {
    string t = to_string(b[i]);
    string w = to_string(c[i]);
    s = replace_c(s, t, w);
    long ans = 0;
    long tmp = 0;
    for (int i = 0; i < (int)(s.size() + 1); i++) {
      if (s[i] >= '0' && s[i] <= '9') {
        tmp *= 10;
        tmp += s[i] - 48;
      }
      if (s[i] == ' ' || s[i] == '\0') {
        ans += tmp;
        tmp = 0;
      }
    }
    cout << ans << endl;
  }
  return (0);
}
