#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int M = 1e4 + 5;
const int Max = (1 << 26) + 5;
short a[Max];
int n, m, b[M], ans[M];
char str[N], sets[30];
int main() {
  scanf("%s", str + 1);
  n = strlen(str + 1);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%s", sets);
    int len = strlen(sets);
    for (int j = 0; j < len; j++) b[i] |= 1 << (sets[j] - 'a');
    a[b[i]] = i;
  }
  for (int i = 1; i <= n; i++)
    for (int j = i, s = 0; j <= n; j++) {
      if (str[j] == str[i - 1]) break;
      s |= 1 << (str[j] - 'a');
      if (!(s >> (str[j + 1] - 'a') & 1)) ans[a[s]]++;
    }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[a[b[i]]]);
  return 0;
}
