#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const double pi = 3.14159265358979323846;
string s;
int k;
bool pr[26][26];
bool read() {
  if (!(cin >> s)) return false;
  cin >> k;
  for (int i = 0; i < int(k); ++i) {
    char a, b;
    cin >> a >> b;
    pr[a - 'a'][b - 'a'] = true;
    pr[b - 'a'][a - 'a'] = true;
  }
  return true;
}
int dp[100001][26];
void solve() {
  for (int i = 0; i < int(100000); ++i)
    for (int j = 0; j < int(26); ++j) dp[i + 1][j] = -1;
  int n = s.size();
  int ans = 0;
  for (int i = 0; i < int(n); ++i) {
    for (int j = 0; j < int(26); ++j) {
      if (!pr[j][s[i] - 'a'])
        dp[i + 1][s[i] - 'a'] = max(dp[i + 1][s[i] - 'a'], dp[i][j] + 1);
      dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
    }
  }
  for (int i = 0; i < int(26); ++i) ans = max(ans, dp[n][i]);
  cout << n - ans << endl;
}
int main() {
  while (read()) solve();
  return 0;
}
