#include <bits/stdc++.h>
using namespace std;
char s[1000005], o[30];
int main() {
  pair<int, int> a[10001];
  int m, n = strlen(gets(s)), p[26], ans[10000] = {};
  sscanf(gets(o), "%d", &m);
  for (int i = 0; i < m; i++) {
    for (auto c = gets(o); *c; c++) a[i].first |= 1 << (*c - 'a');
    a[i].second = i;
  }
  sort(a, a + m);
  a[m] = {1 << 27, 0};
  set<int> u = {-1};
  memset(p, -1, sizeof(p));
  for (int i = 0; i < n; i++) {
    if (s[i + 1] == s[i]) continue;
    if (~p[s[i] - 'a']) u.erase(p[s[i] - 'a']);
    int mask = 1 << (s[i] - 'a');
    for (auto t = u.crbegin();; t++) {
      auto c = lower_bound(a, a + m, pair<int, int>(mask, 0));
      if (c->first == mask) ans[c->second]++;
      if (~*t && s[*t] != s[i + 1])
        mask |= 1 << (s[*t] - 'a');
      else
        break;
    }
    u.insert(p[s[i] - 'a'] = i);
  }
  for (int i = 1; i < m; i++) {
    if (a[i].first != a[i - 1].first) continue;
    ans[a[i].second] = ans[a[i - 1].second];
  }
  for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
}
