#include <bits/stdc++.h>
using namespace std;
bool adj[50][50];
string s;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> s;
  int k;
  cin >> k;
  for (int i = 0; i < k; i++) {
    char a, b;
    cin >> a >> b;
    adj[a - 'a'][b - 'a'] = adj[b - 'a'][a - 'a'] = 1;
  }
  int res = 0;
  int n = s.size();
  for (int i = 0; i < n - 1;) {
    int cur = s[i] - 'a';
    int next = s[i + 1] - 'a';
    int curam = 0;
    int nextam = 0;
    if (adj[cur][next]) {
      int icp = i - 1;
      while ((s[icp] - 'a' == cur or s[icp] - 'a' == next)) {
        if (s[icp] - 'a' == cur)
          curam++;
        else if (s[icp] - 'a' == next)
          nextam++;
        icp--;
      }
      while ((s[i] - 'a' == cur or s[i] - 'a' == next)) {
        if (s[i] - 'a' == cur)
          curam++;
        else if (s[i] - 'a' == next)
          nextam++;
        i++;
      }
      res += min(curam, nextam);
    } else
      i++;
  }
  cout << res;
  return 0;
}
