#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  vector<int> cf(26, -1);
  int k;
  scanf("%d", &k);
  for (int i = 0; i < k; i++) {
    string t;
    cin >> t;
    cf[t[0] - 97] = t[1] - 97;
    cf[t[1] - 97] = t[0] - 97;
  }
  int n = s.length();
  vector<int> len(26, 0);
  for (int i = 0; i < n; i++) {
    int mx = 0;
    for (int j = 0; j < 26; j++) {
      if (cf[s[i] - 97] != j) {
        mx = max(mx, len[j]);
      }
    }
    len[s[i] - 97] = max(len[s[i] - 97], mx + 1);
  }
  cout << n - *max_element(len.begin(), len.end()) << endl;
  return 0;
}
