#include <bits/stdc++.h>
using namespace std;
int n, k;
const int maxn = 3 * 3 * 3 + 1, maxc = 9 * 6 + 1;
short links[1 << 26];
int anse[12000];
int wherec[26];
int so[26];
int wherecinsort[26];
int main() {
  string s, p;
  int numb;
  cin >> s;
  scanf("%d\n", &n);
  vector<int> tans;
  for (int i = 0; i < n; ++i) {
    getline(cin, p);
    numb = 0;
    for (int j = 0; j < p.size(); ++j) numb |= (1 << (p[j] - 'a'));
    links[numb] = i + 1;
    tans.push_back(numb);
  }
  for (int i = 0; i < 26; ++i) {
    wherecinsort[i] = i;
    wherec[i] = -1;
    so[i] = i;
  }
  int c, zap, mask;
  for (int i = 0; i < s.size(); ++i) {
    c = (s[i] - 'a');
    wherec[c] = i;
    while (wherecinsort[c] < 25 && wherec[so[wherecinsort[c] + 1]] < i) {
      swap(so[wherecinsort[c]], so[wherecinsort[c] + 1]);
      swap(wherecinsort[c], wherecinsort[so[wherecinsort[c]]]);
    }
    zap = (i + 1 < s.size() ? s[i + 1] - 'a' : -2);
    mask = 0;
    for (int j = 25; j >= 0; --j) {
      if (so[j] == zap || wherec[so[j]] < 0) break;
      mask |= (1 << so[j]);
      ++anse[links[mask]];
    }
  }
  for (int i = 0; i < n; ++i) printf("%d\n", anse[links[tans[i]]]);
}
