#include <bits/stdc++.h>
using namespace std;
const long long m = 18446744073709551615;
int main() {
  int a, ans = 0;
  cin >> a;
  string s = "";
  for (int i = 0; i < a; i++) {
    char h;
    cin >> h;
    s += h;
    ans += h - '0';
  }
  int r;
  cin >> r;
  for (int i = 0; i < r; i++) {
    char a, b;
    cin >> a >> b;
    while (s.find(a) != m) {
      ans += b - a;
      s[s.find(a)] = b;
    }
    cout << ans << endl;
  }
  return 0;
}
