#include <bits/stdc++.h>
void Get(int &T) {
  char C;
  bool F = 0;
  for (; C = getchar(), C < '0' || C > '9';)
    if (C == '-') F = 1;
  for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0')
    ;
  F && (T = -T);
}
char S[1000005];
int N;
void Init() {
  scanf("%s", S + 1);
  N = strlen(S + 1);
}
void Debug(int S) {
  for (int i = 'a'; i <= 'z'; i++, S >>= 1)
    if (S & 1) printf("%c ", i);
}
int Stack[10005];
int StackTop;
char Pool[201326605];
int M;
void Work() {
  for (int i = N; i >= 1; i--) {
    StackTop++;
    Stack[StackTop] = 0;
    int j = 0;
    for (int k = 1; k <= StackTop; k++) {
      if (((Stack[k] >> (S[i] - 'a')) & 1) == 0) {
        ((*((int *)((Pool) + 3 * Stack[k]))))++;
      }
      Stack[j + 1] = (Stack[k] | (1 << (S[i] - 'a')));
      if (j == 0 || Stack[j] != Stack[j + 1]) {
        j++;
      }
    }
    StackTop = j;
  }
  for (int k = 1; k <= StackTop; k++) {
    ((*((int *)((Pool) + 3 * Stack[k]))))++;
  }
  Get(M);
  for (int i = 1; i <= M; i++) {
    char Str[35];
    scanf("%s", Str + 1);
    int S = 0;
    for (int i = 1; Str[i] != 0; i++) S |= (1 << (Str[i] - 'a'));
    int Ans = ((*((int *)((Pool) + 3 * S)))) & ((1 << 24) - 1);
    printf("%d\n", Ans);
  }
}
int main() {
  Init();
  Work();
  return 0;
}
