#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   int main() {     string s;     cin >> s;     int k;     cin >> k;     unordered_set<string> st;     for (int i = 0; i < k; i++) {       string temp;       cin >> temp;       st.insert(temp);       string temp2 =   ;       temp2 = temp2 + temp[1];       temp2 = temp2 + temp[0];       st.insert(temp2);     }     vector<pair<char, int> > arr;     for (int i = 0; i < s.size(); i++) {       int count = 1;       int j = i + 1;       while (j < s.size() && s[j] == s[i]) {         count++;         j++;       }       arr.push_back(make_pair(s[i], count));       i = j - 1;     }     int ans = 0;     for (int i = 0; i < arr.size() - 1; i++) {       string temp =   ;       temp = temp + arr[i].first;       temp = temp + arr[i + 1].first;       if (st.find(temp) != st.end()) {         int c1 = arr[i].second, c2 = arr[i + 1].second;         int j = i + 2;         while (j < s.size()) {           if (arr[j].first == arr[i].first)             c1 += arr[j].second;           else if (arr[j].first == arr[i + 1].first)             c2 += arr[j].second;           else             break;           j++;         }         ans += min(c1, c2);         i = j - 1;       }     }     cout << ans;     return 0;   }   