#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
int main() {
  string s;
  cin >> s;
  int n = (int)(s).size();
  int k;
  cin >> k;
  map<char, char> a;
  for (int i = 0; i < k; ++i) {
    string s;
    cin >> s;
    a[s[0]] = s[1];
    a[s[1]] = s[0];
  }
  int x = -1;
  vector<int> pr(n);
  int last[26];
  memset((last), (-1), sizeof(last));
  vector<int> dp(n, 0);
  for (int i = 0; i < n; ++i) {
    dp[i] = 1;
    if (i == 0) {
      last[s[i] - 'a'] = 1;
      continue;
    }
    char x = a[s[i]];
    int y = -1;
    for (int j = 0; j < 26; ++j) {
      if (j != x - 'a') {
        y = max(y, last[j]);
      }
    }
    if (y == -1) {
      last[s[i] - 'a'] = max(last[s[i] - 'a'], 1);
      continue;
    }
    dp[i] = max(dp[i], y + 1);
    last[s[i] - 'a'] = max(dp[i], last[s[i] - 'a']);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans = max(ans, dp[i]);
  }
  cout << n - ans;
  return 0;
}
