#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000100;
char S[maxn], s[10010];
static int tc, m, que[maxn], lq;
static int mk[maxn], ans[maxn];
unsigned short loc[1 << 26];
int main() {
  gets(S + 1);
  int len = strlen(S + 1);
  memset(loc, -1, sizeof(loc));
  scanf("%d\n", &m);
  for (int i = 1; i <= m; i++) {
    gets(s + 1);
    int ls = strlen(s + 1);
    for (int j = 1; j <= ls; j++) mk[i] |= 1 << s[j] - 'a';
    if (loc[mk[i]] > m) loc[mk[i]] = ++tc;
  }
  lq = 0;
  for (int i = 1; i <= len + 1; i++) {
    int tk = 0, ts = 0;
    for (int j = 1; j <= lq && que[j] != S[i] - 'a'; j++) {
      tk += 1 << que[j];
      ts = j;
      if (loc[tk] <= m) ans[loc[tk]]++;
    }
    ts++;
    if (lq < ts) lq = ts;
    for (int j = ts; j > 1; j--) que[j] = que[j - 1];
    que[1] = S[i] - 'a';
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[loc[mk[i]]]);
  return 0;
}
