#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> mp;
vector<int> masks;
int main() {
  cin.tie(nullptr), ios_base::sync_with_stdio(false);
  string s;
  cin >> s;
  int m;
  cin >> m;
  while (m--) {
    string sc;
    cin >> sc;
    int mask = 0;
    for (char c : sc) mask |= 1 << (c - 'a');
    mp[mask] = 0;
    masks.push_back(mask);
  }
  tuple<int, int, int> last[26];
  for (int i = 0; i < 26; i++) last[i] = {-1, 0, i};
  for (int i = 0; i < int(s.size()); i++) {
    last[s[i] - 'a'] = {i, 1 << (s[i] - 'a'), int(s[i] - 'a')};
    sort(last, last + 26, greater<decltype(last[0])>());
    int mask = 0;
    for (int j = 0; j < 26; j++) {
      mask |= 1 << get<2>(last[j]);
      if (i + 1 < int(s.size()) && s[i + 1] - 'a' == get<2>(last[j])) break;
      if (get<0>(last[j]) < 0) break;
      if (mp.find(mask) != mp.end()) {
        mp[mask]++;
      }
    }
    sort(last, last + 26, [](tuple<int, int, int> l, tuple<int, int, int> r) {
      return get<2>(l) < get<2>(r);
    });
  }
  for (int mask : masks) cout << mp[mask] << '\n';
}
