#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5143;
const long long Linf = 1e18 + 5413;
const double eps = 1e-10;
const double pi = acos(-1);
template <class T>
inline void umax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
inline void umin(T &a, T b) {
  if (a > b) a = b;
}
template <class T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <class T>
inline T lcm(T a, T b) {
  return a / __gcd(a, b) * b;
}
inline int read() {
  int res = 0LL;
  int neg;
  while (1) {
    char ch = getchar();
    if (ch >= '0' && ch <= '9' || ch == '-') {
      if (ch == '-')
        neg = -1;
      else
        neg = 1, res = ch - '0';
      break;
    }
  }
  while (1) {
    char ch = getchar();
    if (ch >= '0' && ch <= '9')
      res *= 10, res += ch - '0';
    else
      break;
  }
  return res * neg;
}
const int N = 1313131;
const int M = 11717;
char buf[77];
char s[N];
int ask[M];
map<int, bool> isquery;
map<int, int> ans;
int main() {
  scanf(" %s", s);
  int n = strlen(s);
  int m = read();
  for (int(i) = (0); (i) < (m); (i)++) {
    scanf(" %s", buf);
    int len = strlen(buf);
    int mask = 0;
    for (int(j) = (0); (j) < (len); (j)++) mask |= (1LL << (buf[j] - 'a'));
    ask[i] = mask;
    isquery[mask] = true;
  }
  vector<char> left;
  s[n + 1] = '$';
  for (int(i) = (0); (i) < (n); (i)++) {
    char next = s[i + 1];
    if (next != s[i]) {
      int mask = (1LL << (s[i] - 'a'));
      if (isquery.count(mask) > 0) ans[mask]++;
      for (int(j) = (left.size() - 1); (j) >= (0); (j)--) {
        char c = left[j];
        if (c == next) break;
        if (c == s[i]) continue;
        mask |= (1LL << (c - 'a'));
        if (isquery.count(mask) > 0) ans[mask]++;
      }
      for (int(j) = (0); (j) < (left.size()); (j)++)
        if (left[j] == s[i]) {
          left.erase(left.begin() + j);
          break;
        }
      left.push_back(s[i]);
    }
  }
  for (int(i) = (0); (i) < (m); (i)++) printf("%d\n", ans[ask[i]]);
  return 0;
}
