#include <bits/stdc++.h>
using namespace std;
string s;
int f[30];
int main(void) {
  int n, i, m, x, y;
  memset(f, -1, sizeof(f));
  cin >> s >> n;
  for (i = 0; i < n; i++) {
    string fs;
    cin >> fs;
    x = fs[0] - 'a';
    y = fs[1] - 'a';
    f[x] = y;
    f[y] = x;
  }
  int a[30];
  memset(a, -1, sizeof(a));
  m = ((int)s.size());
  for (i = 0; i < m; i++) {
    int x = s[i] - 'a';
    if (a[x] == -1) a[x] = i;
    for (y = 0; y < 26; y++)
      if (a[y] != -1 && f[x] != y) a[x] = min(a[x], a[y]);
    for (y = 0; y < 26; y++)
      if (y != x && a[y] != -1) a[y]++;
  }
  int ans = m - 1;
  for (i = 0; i < 26; i++) {
    if (a[i] != -1) ans = min(ans, a[i]);
  }
  cout << ans << endl;
}
