#include <bits/stdc++.h>
using namespace std;
string str(int i) {
  char s[32];
  sprintf(s, "%d", i);
  return s;
}
template <typename T>
void uniq(vector<T>& v) {
  sort(ALL(v));
  v.erase(unique(ALL(v)), v.end());
}
template <typename T>
struct dual {
  T x, y;
  dual() {}
  dual(const T& x, const T& y) : x(x), y(y) {}
  bool operator<(const dual<T>& r) const {
    return x < r.x || x == r.x && y < r.y;
  }
  dual operator+(const dual<T>& r) { return dual(x + r.x, y + r.y); }
  dual operator-(const dual<T>& r) { return dual(x - r.x, y - r.y); }
  T operator*(const dual<T>& r) { return x * r.x + y * r.y; }
  T operator%(const dual<T>& r) { return x * r.y - y * r.x; }
};
inline int pack(const char* s) {
  int ret = 0;
  while (*s != '\0') {
    ret |= 1 << (*s - 'a');
    ++s;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  char s[1000000 + 1];
  cin >> s;
  int n = strlen(s);
  for (int i = 0; i < n; i++) {
    s[i] -= 'a';
  }
  s[n] = 0xFF;
  int next[n][26];
  unordered_map<int, int> cnt;
  for (int i = 0; i < 26; i++) {
    if (i == s[n - 1]) {
      next[n - 1][i] = n - 1;
    } else {
      next[n - 1][i] = -1;
    }
  }
  for (int i = n - 2; i >= 0; i--) {
    memcpy(next[i], next[i + 1], sizeof next[i]);
    next[i][s[i]] = i;
  }
  int m;
  cin >> m;
  int query[m];
  set<int> query_table;
  for (int i = 0; i < m; i++) {
    char buf[32];
    cin >> buf;
    query[i] = pack(buf);
    query_table.insert(query[i]);
  }
  vector<dual<int>> v;
  for (int i = 0; i < n; i++) {
    v.clear();
    int* nx = next[i];
    for (int j = 0; j < 26; j++) {
      if (nx[j] != -1) {
        v.push_back({nx[j], j});
      }
    }
    sort((v).begin(), (v).end());
    int mk = 0;
    for (auto& x : v) {
      mk |= 1 << x.y;
      if (i > 0 && (mk >> s[i - 1] & 1)) {
        break;
      }
      if (query_table.count(mk)) ++cnt[mk];
    }
  }
  for (int i = 0; i < m; i++) {
    cout << cnt[query[i]] << '\n';
  }
  return 0;
}
