#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
int n, cs;
int la[26], sav[26 * N];
char s[N];
void gao(int l, int r) {
  if (l > r) return;
  int mask = 0;
  for (int k = (0); k < (26); k++)
    if (la[k] >= l) mask |= (1 << k);
  sav[++cs] = mask;
}
int main() {
  while (~scanf("%s", s + 1)) {
    memset(la, 0, sizeof(la));
    n = strlen(s + 1);
    cs = 0;
    for (int i = (1); i < (n + 1); i++) {
      la[s[i] - 'a'] = i;
      int _ = s[i + 1] - 'a';
      if (i == n || !la[_]) gao(1, i);
      for (int j = (0); j < (26); j++)
        if (la[j]) {
          if (i == n || la[_] <= la[j]) gao(la[j] + 1, i);
        }
    }
    sort(sav + 1, sav + 1 + cs);
    int q;
    scanf("%d", &q);
    while (q--) {
      scanf("%s", s);
      int mask = 0;
      for (int i = 0; s[i]; ++i) mask |= (1 << (s[i] - 'a'));
      int l = lower_bound(sav + 1, sav + 1 + cs, mask) - sav;
      int r = upper_bound(sav + 1, sav + 1 + cs, mask) - sav - 1;
      int ans = max(0, r - l + 1);
      printf("%d\n", ans);
    }
  }
  return 0;
}
