#include <bits/stdc++.h>
using namespace std;
int main() {
  string str;
  getline(cin, str);
  int N;
  cin >> N;
  if (N == 0) {
    cout << 0 << endl;
    return 0;
  }
  int const MAXC = 26;
  int badch[MAXC];
  for (int i = 0; i < MAXC; i++) badch[i] = -1;
  string str2;
  getline(cin, str2);
  for (int i = 0; i < N; i++) {
    getline(cin, str2);
    badch[str2[0] - 'a'] = str2[1] - 'a';
    badch[str2[1] - 'a'] = str2[0] - 'a';
  }
  int cnt = 0;
  for (int i = 0; i < str.size() - 1;) {
    char ch1 = str[i];
    char ch2 = str[i + 1];
    if ((ch1 == ch2 && badch[ch1 - 'a'] != -1) ||
        (badch[ch1 - 'a'] == ch2 - 'a')) {
      map<int, int> cnts;
      cnts[ch1 - 'a']++;
      cnts[ch2 - 'a']++;
      int j = i + 2;
      for (; j < str.size(); j++) {
        char ch3 = str[j];
        if (ch3 == ch1 || (ch3 - 'a' == badch[ch1 - 'a']))
          cnts[ch3 - 'a']++;
        else
          break;
      }
      int cnt1 = cnts.begin()->second;
      int cnt2 = 0;
      if (cnts.size() > 1) cnt2 = (++cnts.begin())->second;
      cnt += min(cnt1, cnt2);
      i = j;
    } else
      i++;
  }
  cout << cnt << endl;
  return 0;
}
