#include <bits/stdc++.h>
using namespace std;
short a[1 << 26];
int b[10001], c[10001], n, m, i, j, k, l;
char s[1 << 20], str[27];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  cin >> m;
  for (i = 1; i <= m; i++) {
    scanf("%s", str);
    l = strlen(str);
    for (j = k = 0; j < l; j++) k |= 1 << str[j] - 'a';
    a[k] = i, b[i] = k;
  }
  for (i = 1; i <= n; i++)
    for (j = i, k = 0; j <= n; j++) {
      if (s[j] == s[i - 1]) break;
      k |= 1 << s[j] - 'a';
      if (!(k >> s[j + 1] - 'a' & 1)) c[a[k]]++;
    }
  for (i = 1; i <= m; i++) printf("%d\n", c[a[b[i]]]);
  return 0;
}
