#include <bits/stdc++.h>
using namespace std;
vector<int> ans;
vector<int> last_idx, now_idx;
deque<int> dq;
int32_t main() {
  {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
  };
  string s;
  cin >> s;
  for (int i = s.size() - 1; i >= 0; i--) {
    for (auto it = dq.begin(); it != dq.end(); it++) {
      if (*it == s[i] - 'a') {
        dq.erase(it);
        break;
      }
    }
    dq.push_front(s[i] - 'a');
    vector<pair<int, int> > v;
    int idx = 0;
    now_idx.clear();
    for (int j : dq) {
      idx |= 1 << j;
      auto it = lower_bound(last_idx.begin(), last_idx.end(), idx);
      if (it == last_idx.end() || *it != idx) ans.push_back(idx);
      now_idx.push_back(idx);
    }
    last_idx = now_idx;
  }
  sort(ans.begin(), ans.end());
  int m;
  cin >> m;
  while (m--) {
    cin >> s;
    int idx = 0;
    for (char i : s) idx |= 1 << (i - 'a');
    cout << (int)(upper_bound(ans.begin(), ans.end(), idx) -
                  lower_bound(ans.begin(), ans.end(), idx))
         << '\n';
  }
  return 0;
}
