#include <bits/stdc++.h>
using namespace std;
short lst[1 << 26];
int mask[10005], ans[10005];
char str[1000005], tmp[35];
int main() {
  scanf("%s", str);
  int n = strlen(str);
  int m;
  scanf("%d", &m);
  memset(lst, -1, sizeof(lst));
  for (int i = 0; i < m; i++) {
    scanf("%s", tmp);
    int len = strlen(tmp), app = 0;
    for (int j = 0; j < len; j++) app |= 1 << tmp[j] - 'a';
    lst[app] = i;
    mask[i] = app;
  }
  for (int i = 0; i < n; i++) {
    int x = 0;
    for (int j = i; j < n; j++) {
      if (i && str[j] == str[i - 1]) break;
      x |= 1 << str[j] - 'a';
      if ((x >> str[j + 1] - 'a') & 1 ^ 1 && ~lst[x]) ans[lst[x]]++;
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", ans[lst[mask[i]]]);
  return 0;
}
