import
java.util.*;
public
class
Main {
public
static
void
firstOccurrence(String s,
ArrayList<Pair<Pair<Integer, Integer>, Character>> Q)
{
int
N = s.length();
int
M = Q.size();
HashMap<Character, ArrayList<Integer>> v
=
new
HashMap<Character, ArrayList<Integer>>();
for
(
int
i =
0
; i < N; i++) {
char
c = s.charAt(i);
if
(!v.containsKey(c)) {
v.put(c,
new
ArrayList<Integer>());
}
v.get(c).add(i);
}
for
(
int
i =
0
; i < M; i++) {
int
left = Q.get(i).first.first;
int
right = Q.get(i).first.second;
char
c = Q.get(i).second;
if
(!v.containsKey(c) || v.get(c).size() ==
0
) {
System.out.print(
"-1 "
);
continue
;
}
ArrayList<Integer> charIndices = v.get(c);
int
idx = Collections.binarySearch(charIndices, left);
if
(idx <
0
) {
idx = -(idx +
1
);
if
(idx == charIndices.size() || charIndices.get(idx) > right) {
System.out.print(
"-1 "
);
continue
;
}
}
else
if
(charIndices.get(idx) > right) {
System.out.print(
"-1 "
);
continue
;
}
idx = charIndices.get(idx);
if
(idx > right) {
System.out.print(
"-1 "
);
}
else
{
System.out.print(idx +
" "
);
}
}
}
public
static
void
main(String[] args) {
String S =
"abcabcabc"
;
ArrayList<Pair<Pair<Integer, Integer>, Character>> Q
=
new
ArrayList<Pair<Pair<Integer, Integer>, Character>>();
Q.add(
new
Pair<Pair<Integer, Integer>, Character>(
new
Pair<Integer, Integer>(
0
,
3
),
'a'
));
Q.add(
new
Pair<Pair<Integer, Integer>, Character>(
new
Pair<Integer, Integer>(
0
,
2
),
'b'
));
Q.add(
new
Pair<Pair<Integer, Integer>, Character>(
new
Pair<Integer, Integer>(
2
,
4
),
'z'
));
firstOccurrence(S, Q);
}
}
class
Pair<U, V> {
public
U first;
public
V second;
public
Pair(U first, V second) {
this
.first = first;
this
.second = second;
}
}