#include <algorithm>
#include <array>
#include <cassert>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;

template <int MOD_> struct modnum {
	static constexpr int MOD = MOD_;
	static_assert(MOD_ > 0, "MOD must be positive");

private:
	using ll = long long;

	int v;

	static int minv(int a, int m) {
		a %= m;
		assert(a);
		return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
	}

public:

	modnum() : v(0) {}
	modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
	explicit operator int() const { return v; }
	friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
	friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }

	friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
	friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }

	modnum inv() const {
		modnum res;
		res.v = minv(v, MOD);
		return res;
	}
	modnum neg() const {
		modnum res;
		res.v = v ? MOD-v : 0;
		return res;
	}

	modnum operator- () const {
		return neg();
	}
	modnum operator+ () const {
		return modnum(*this);
	}

	modnum& operator ++ () {
		v ++;
		if (v == MOD) v = 0;
		return *this;
	}
	modnum& operator -- () {
		if (v == 0) v = MOD;
		v --;
		return *this;
	}
	modnum& operator += (const modnum& o) {
		v += o.v;
		if (v >= MOD) v -= MOD;
		return *this;
	}
	modnum& operator -= (const modnum& o) {
		v -= o.v;
		if (v < 0) v += MOD;
		return *this;
	}
	modnum& operator *= (const modnum& o) {
		v = int(ll(v) * ll(o.v) % MOD);
		return *this;
	}
	modnum& operator /= (const modnum& o) {
		return *this *= o.inv();
	}

	friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
	friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
	friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
	friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
	friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
	friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};

template <typename T> T pow(T a, long long b) {
	assert(b >= 0);
	T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}

const int STRING_SZ = 200000;
typedef modnum<998244353> mnum1;
typedef modnum<1000000007> mnum2;
typedef pair<mnum1, mnum2> shash;
const mnum1 HASH1 = 3137;
const mnum2 HASH2 = 1009;
mnum1 hash1pows[STRING_SZ+1];
mnum2 hash2pows[STRING_SZ+1];
shash hashes[STRING_SZ+1];
string s;
void init() {
  hash1pows[0] = 1;
  for(int i = 1; i <= STRING_SZ; i++) hash1pows[i] = hash1pows[i-1] * HASH1;
  hash2pows[0] = 1;
  for(int i = 1; i <= STRING_SZ; i++) hash2pows[i] = hash2pows[i-1] * HASH2;
}
inline pair<int, int> getHash(int lhs, int rhs) {
  mnum1 a = hashes[rhs].first - hashes[lhs].first * hash1pows[rhs-lhs];
  mnum2 b = hashes[rhs].second - hashes[lhs].second * hash2pows[rhs-lhs];
  return {(int)a, (int)b};
}
void genHash() {
  for(int i = 0; i < (int)s.size(); i++) {
    hashes[i+1].first = hashes[i].first * HASH1 + s[i];
    hashes[i+1].second = hashes[i].second * HASH2 + s[i];
  }
}

vector<pair<string, pair<int, int>>> queries[200005]; // string, <k, idx>
int ret[200005];
int main() {
  cin >> s;
  int q;
  cin >> q;
  for(int i = 0; i < q; i++) {
    string ss;
    int k;
    cin >> ss >> k;
    queries[ss.size()].emplace_back(ss, make_pair(--k, i));
  }
  memset(ret, -1, sizeof(ret));
  init();
  genHash();
  for(int a = 1; a <= (int)s.size(); a++) {
    if(queries[a].size() == 0) continue;
    int amt = 0;
    map<pair<int, int>, vector<int>> dp;
    for(int b = a; b <= (int)s.size(); b++) {
      auto h = getHash(b - a, b);
      dp[make_pair((int)h.first, (int)h.second)].push_back(b-a+1);
    }
    for(auto qry: queries[a]) {
      mnum1 lhs = 0;
      mnum2 rhs = 0;
      for(auto ch: qry.first) {
        lhs *= HASH1; lhs += ch;
        rhs *= HASH2; rhs += ch;
      }
      array<int, 3> qrya = {(int)lhs, (int)rhs, 0};
      if(!dp.count({qrya[0], qrya[1]})) continue;
      vector<int>& idx = dp[{qrya[0], qrya[1]}];
      if(qry.second.first < (int)idx.size()) ret[qry.second.second] = idx[qry.second.first];
    }
  }
  for(int i = 0; i < q; i++) cout << ret[i] << "\n";
}