#include <bits/stdc++.h>
using namespace std;
char V[1000010], buf[100];
int L, M;
int Q[10010];
map<int, pair<int, int> > cnt;
int nxt[26];
bool compare(const int& f, const int& s) { return nxt[f] < nxt[s]; }
int main() {
  scanf("%s", &V);
  L = strlen(V);
  scanf("%d", &M);
  for (int i = 0; i < M; ++i) {
    scanf("%s", &buf);
    int BL = strlen(buf);
    int msk = 0;
    for (int j = 0; j < BL; ++j) msk |= 1 << (buf[j] - 'a');
    Q[i] = msk;
    cnt[msk] = pair<int, int>(L, 0);
  }
  int avb[26];
  for (int i = 0; i < 26; ++i) avb[i] = i;
  for (int i = 0; i < 26; ++i) nxt[i] = L;
  for (int i = L - 1; i >= 0; --i) {
    if (i + 1 < L && V[i] == V[i + 1]) continue;
    nxt[V[i] - 'a'] = i;
    sort(avb, avb + 26, compare);
    int cm = 0;
    for (int j = 0; j < 26; ++j) {
      int T = avb[j];
      if (nxt[T] == L) break;
      cm |= (1 << T);
      if (cnt.count(cm)) {
        pair<int, int>& U = cnt[cm];
        if (nxt[T] < U.first) ++U.second;
        U.first = i;
      }
    }
  }
  for (int i = 0; i < M; ++i) printf("%d\n", cnt[Q[i]].second);
  return 0;
}
