#include <bits/stdc++.h>
using namespace std;
char buf[1000010];
string s;
int Q;
int query[10010];
int ans[10010];
short id[(1 << 26)];
void add(int x) {
  if (id[x] != -1) ans[id[x]]++;
}
int main(void) {
  int i, j;
  scanf("%s", buf);
  s = buf;
  int N = s.length();
  scanf("%d", &Q);
  for ((i) = 0; (i) < (int)(Q); (i)++) {
    scanf("%s", buf);
    string t = buf;
    int mask = 0;
    for ((j) = 0; (j) < (int)(t.length()); (j)++) mask |= (1 << (t[j] - 'a'));
    query[i] = mask;
  }
  for ((i) = 0; (i) < (int)((1 << 26)); (i)++) id[i] = -1;
  for ((i) = 0; (i) < (int)(Q); (i)++)
    if (id[query[i]] == -1) id[query[i]] = i;
  string prev = "";
  for (i = N - 1; i >= 0; i--) {
    char ch = s[i];
    int x = -1;
    for ((j) = 0; (j) < (int)(prev.length()); (j)++)
      if (prev[j] == ch) x = j;
    string next;
    if (x == -1) {
      next = ch + prev;
    } else {
      next = ch + prev.substr(0, x) + prev.substr(x + 1);
    }
    int mask1 = 0, mask2 = 0;
    for ((j) = 0; (j) < (int)(prev.length()); (j)++) {
      mask1 |= (1 << (prev[j] - 'a'));
      if (j < next.length()) mask2 |= (1 << (next[j] - 'a'));
      if (mask1 != mask2) add(mask1);
    }
    if (i == 0) {
      int mask3 = 0;
      for ((j) = 0; (j) < (int)(next.length()); (j)++) {
        mask3 |= (1 << (next[j] - 'a'));
        add(mask3);
      }
    }
    prev = next;
  }
  for ((i) = 0; (i) < (int)(Q); (i)++)
    if (id[query[i]] != i) ans[i] = ans[id[query[i]]];
  for ((i) = 0; (i) < (int)(Q); (i)++) cout << ans[i] << endl;
  return 0;
}
