#include <bits/stdc++.h>
const int oo = 1 << 30;
using namespace std;
int f[110000][26], b[30][30], N, M, i, j, k, ans;
char s[110000], s1[5], s2[5];
int main() {
  scanf("%s%d", s + 1, &M);
  while (M--) {
    scanf("%1s%1s", s1, s2);
    j = s1[0] - 'a';
    k = s2[0] - 'a';
    b[j][k] = b[k][j] = 1;
  }
  N = strlen(s + 1);
  for (i = 1; i <= N; i++) s[i] = s[i] - 'a';
  for (j = 0; j < 26; j++) f[0][j] = oo;
  for (i = 1; i <= N; i++) {
    k = s[i];
    for (j = 0; j < 26; j++) f[i][j] = f[i - 1][j] + 1;
    f[i][k] = min(f[i][k], i - 1);
    for (j = 0; j < 26; j++)
      if (!b[j][k]) f[i][k] = min(f[i][k], f[i - 1][j]);
  }
  ans = oo;
  for (j = 0; j < 26; j++) ans = min(ans, f[N][j]);
  printf("%d\n", ans);
}
