#include <bits/stdc++.h>
using namespace std;
int k;
bool d[255][255];
string s;
int main() {
  cin >> s;
  cin >> k;
  char ch1, ch2;
  for (int i = 0; i < k; ++i) {
    cin >> ch1 >> ch2;
    d[ch1][ch2] = true;
    d[ch2][ch1] = true;
  }
  int ans = 0;
  for (int i = 0; i < s.size() - 1; ++i) {
    if (!d[s[i]][s[i + 1]]) continue;
    char ch1 = s[i], ch2 = s[i + 1];
    int t1 = 1, t2 = 1;
    int j = i;
    while (j - 1 >= 0 && (s[j - 1] == ch1 || s[j - 1] == ch2)) {
      if (s[j - 1] == ch1)
        t1++;
      else
        t2++;
      --j;
    }
    j = i + 1;
    while (j + 1 < s.size() && (s[j + 1] == ch1 || s[j + 1] == ch2)) {
      if (s[j + 1] == ch1)
        t1++;
      else
        t2++;
      ++j;
    }
    i = j;
    ans += min(t1, t2);
  }
  cout << ans;
  return 0;
}
