#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
char s[maxn];
int nxt[maxn][26], query[10005], code[maxn][26], n, q;
int num[26], ord[26];
unordered_map<int, int> ans;
bool cmp(int x, int y) { return num[x] < num[y]; }
int main() {
  char t[30];
  int ret;
  scanf("%s%d", s, &q);
  n = strlen(s);
  for (int i = 1; i <= q; i++) {
    scanf("%s", t);
    ret = 0;
    for (int j = 0; t[j]; j++) ret |= (1 << (t[j] - 'a'));
    query[i] = ret;
    ans[ret] = 1;
  }
  for (int i = n - 1; i >= 0; i--)
    for (int j = 0; j < 26; j++) {
      if (i == n - 1) {
        if (s[i] != j + 'a')
          nxt[i][j] = n;
        else
          nxt[i][j] = i;
      } else {
        if (s[i] != j + 'a')
          nxt[i][j] = nxt[i + 1][j];
        else
          nxt[i][j] = i;
      }
    }
  for (int j = 0; j < 26; j++) ord[j] = j;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 26; j++) num[j] = nxt[i][j];
    sort(ord, ord + 26, cmp);
    int cc = 0;
    for (int _ = 0; _ < 26; _++) {
      if (num[ord[_]] == n) break;
      cc |= (1 << ord[_]);
      code[i][_] = cc;
      if (i == 0 || code[i][_] != code[i - 1][_]) {
        if (ans.find(cc) != ans.end()) ans[cc]++;
      }
    }
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[query[i]] - 1);
  return 0;
}
