#include <bits/stdc++.h>
using namespace std;
int scan() {
  int t = 0;
  char c;
  c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') {
    t = (t << 3) + (t << 1) + c - '0';
    c = getchar();
  }
  return (t);
}
int main() {
  char str[100004], f, s, prev;
  int n, a[500] = {0}, ans = 0, l, c1, c2;
  gets(str);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("\n%c%c", &f, &s);
    a[f] = s;
    a[s] = f;
  }
  l = strlen(str);
  prev = 0;
  for (int i = 0; i < l; i++) {
    if (str[i] == prev)
      c1++;
    else if (str[i] == a[prev])
      c2++;
    else {
      prev = str[i];
      ans += min(c1, c2);
      c1 = 1;
      c2 = 0;
    }
  }
  ans += min(c1, c2);
  printf("%d\n", ans);
  return (0);
}
