#include <bits/stdc++.h>
using namespace std;
int mask[10000], ans[10000], q[26];
short loc[1 << 26];
char str[1000001];
int main() {
  scanf("%s", str);
  int n = strlen(str), m;
  scanf("%d", &m);
  memset(loc, -1, sizeof(loc));
  int cur = 0;
  for (int i = 0; i < m; i++) {
    char tmp[27];
    scanf("%s", tmp);
    int t = strlen(tmp);
    mask[i] = 0;
    for (int j = 0; j < t; j++) mask[i] += 1 << tmp[j] - 'a';
    if (loc[mask[i]] == -1) loc[mask[i]] = cur++;
  }
  int l = 0;
  for (int i = 0; i <= n; i++) {
    int c = (i < n) ? str[i] - 'a' : -1;
    int tmp = 0, pos = l;
    for (int j = 0; j < l; j++) {
      if (q[j] == c) {
        pos = j;
        break;
      }
      tmp += 1 << q[j];
      if (loc[tmp] != -1) ans[loc[tmp]]++;
    }
    if (i < n) {
      if (pos == l) l++;
      for (int j = pos - 1; j >= 0; j--) q[j + 1] = q[j];
      q[0] = c;
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", ans[loc[mask[i]]]);
  return 0;
}
