#include <bits/stdc++.h>
using namespace std;
constexpr int M = 1e4 + 5;
map<int, int> hashed;
int query[M];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  int m;
  cin >> s >> m;
  for (int i = 0; i < m; ++i) {
    string t;
    cin >> t;
    int hashnum = 0;
    for (char ch : t) hashnum |= 1 << (ch - 'a');
    query[i] = hashnum;
    hashed[hashnum] = 0;
  }
  for (int i = 0; i < s.length(); ++i)
    for (int j = i, hashnum = 0; j < s.length(); ++j) {
      if (s[i - 1] == s[j]) break;
      hashnum |= 1 << (s[j] - 'a');
      if (~hashnum >> (s[j + 1] - 'a') & 1)
        if (hashed.find(hashnum) != hashed.end()) ++hashed[hashnum];
    }
  for (int i = 0; i < m; ++i) cout << hashed[query[i]] << '\n';
}
