#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  cin >> n;
  char ch;
  string s;
  for (long long int i = 0; i < n; i++) {
    cin >> ch;
    s = s + ch;
  }
  long long int q;
  cin >> q;
  while (q--) {
    long long int sum = 0;
    char b, c;
    cin >> b >> c;
    string tp;
    for (long long int i = 0; i < n; i++) {
      if (s[i] == b)
        tp = tp + c;
      else
        tp = tp + s[i];
    }
    for (long long int i = 0; i < n; i++) {
      int lk = tp[i] - 48;
      sum = sum + lk;
    }
    cout << sum << endl;
  }
}
