#include <bits/stdc++.h>
using namespace std;
char s[100111], ch0, ch1;
int m[30][30], f[10][30][30], n;
void up(int &a, int b) {
  if (b < a) a = b;
}
int main() {
  gets(s);
  scanf("%d", &n);
  getchar();
  for (int i = 1; i <= n; i++) {
    ch0 = getchar();
    ch1 = getchar();
    m[ch0 - 'a'][ch1 - 'a'] = 1;
    m[ch1 - 'a'][ch0 - 'a'] = 1;
    getchar();
  }
  int l = strlen(s);
  for (int i = 0; i < 2; i++)
    for (int p = 0; p <= 26; p++)
      for (int q = 0; q <= 26; q++) f[i][p][q] = 1000000;
  f[0][26][s[0] - 'a'] = 0;
  f[0][26][26] = 1;
  for (int i = 0; i < l - 1; i++) {
    for (int p = 0; p <= 26; p++)
      for (int q = 0; q <= 26; q++) f[(i + 1) % 2][p][q] = 1000000;
    for (int p = 0; p <= 26; p++)
      for (int q = 0; q <= 26; q++)
        if (m[p][q] == 0 && f[i % 2][p][q] != 1000000) {
          int k = s[i + 1] - 'a';
          if (m[q][k] == 0) up(f[(i + 1) % 2][q][k], f[i % 2][p][q]);
          if (m[p][k] == 0) up(f[(i + 1) % 2][p][k], f[i % 2][p][q] + 1);
          up(f[(i + 1) % 2][p][q], f[i % 2][p][q] + 1);
        }
  }
  int ans = 1000000;
  for (int p = 0; p <= 26; p++)
    for (int q = 0; q <= 26; q++) up(ans, f[(l - 1) % 2][p][q]);
  printf("%d\n", ans);
  return 0;
}
