#include <bits/stdc++.h>
using namespace std;
char s[1111111];
int cnt[30];
int ans[31111111];
int main() {
  scanf("%s", s);
  int i, j, now, p, t = 0, n;
  for (i = 1; i <= 26; i++) {
    for (j = 0; j < 26; j++) cnt[j] = 0;
    j = i, now = 0;
    for (p = 0; j && s[p]; p++)
      if (!cnt[s[p] - 'a']++) {
        j--;
        now += (1 << (s[p] - 'a'));
      }
    if (j) break;
    for (; s[p] && cnt[s[p] - 'a']; p++) cnt[s[p] - 'a']++;
    ans[t++] = now;
    for (j = 0; s[j]; j++)
      if (!--cnt[s[j] - 'a']) {
        now -= (1 << (s[j] - 'a'));
        for (; s[p] && cnt[s[p] - 'a']++; p++)
          ;
        if (!s[p])
          break;
        else
          ans[t++] = (now += (1 << (s[p++] - 'a')));
        for (; s[p] && cnt[s[p] - 'a']; p++) cnt[s[p] - 'a']++;
      }
  }
  sort(ans, ans + t);
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s", s);
    for (j = 0; j < 26; j++) cnt[j] = 0;
    now = 0;
    for (p = 0; s[p]; p++)
      if (!cnt[s[p] - 'a']++) now += (1 << (s[p] - 'a'));
    printf("%d\n",
           upper_bound(ans, ans + t, now) - lower_bound(ans, ans + t, now));
  }
  return 0;
}
