#include <bits/stdc++.h>
using std::map;
int n, m;
char s[1000010], r[30];
int que[10010];
map<int, int> G;
int main() {
  int i, p, t, j, l;
  map<int, int>::iterator it;
  gets(s), n = strlen(s);
  for (i = 0; i < n; ++i) s[i] -= 'a';
  scanf("%d\n", &m);
  for (i = 0; i < m; ++i) {
    gets(r), l = strlen(r);
    for (j = 0; j < l; ++j) que[i] |= 1 << r[j] - 'a';
    G[que[i]] = 0;
  }
  for (i = 0; i < n; ++i) {
    if (i && s[i] == s[i - 1]) continue;
    p = i ? s[i - 1] : 26, t = 1 << s[i];
    for (j = i + 1; j <= n; ++j) {
      if (j != n && ((t >> s[j]) & 1)) continue;
      if (it = G.find(t), it != G.end()) ++G[t];
      if (s[j] == p) break;
      t |= 1 << s[j];
    }
  }
  for (i = 0; i < m; ++i) printf("%d\n", G[que[i]]);
  return 0;
}
