extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::f32::MAX;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut, SubAssign, Range};
use std::collections::{BTreeMap, VecDeque, BinaryHeap, BTreeSet};
use std::fmt::{Display, Formatter, Error};

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
trait Monotonically<Domain, CoDomain> {
    fn first_true<F: Fn(CoDomain) -> bool>(&self, predicate: F) -> Option<Domain>;
}
impl Monotonically<usize, usize> for Range<usize> {
    fn first_true<F: Fn(usize) -> bool>(&self, predicate: F) -> Option<usize> {
        let mut left = self.start;
        let mut right = self.end;
        while left < right {
            let mid = (left + right) / 2;
            if predicate(mid){
                right = mid;
            }else {
                left = mid + 1;
            }
        }
        if right == self.end {
            None
        }else {
            Some(right)
        }
    }
}
struct MinSegmentTree<T> {
    vec: Vec<Vec<T>>
}
impl <T: Ord + Copy> MinSegmentTree<T> {
    fn create_from(vec: &Vec<T>) -> MinSegmentTree<T> {
        let mut v = vec![vec.clone()];
        let mut len = vec.len();
        while len > 1 {
            len /= 2;
            let mut new_value = Vec::with_capacity(len);
            for i in 0 .. len {
                new_value.push(min(v[v.len() - 1][i * 2], v[v.len() - 1][i * 2 + 1]));
            }
            v.push(new_value);
        }
        MinSegmentTree{vec: v}
    }
    fn min_of(&self, mut from: usize, until: usize) -> T {
        let mut depth = 0;
        let mut position = from;
        let mut to = until - 1;
        for i in 0 .. self.vec.len() {
            if from % 2 == 1 && self.vec[i][from] < self.vec[depth][position] {
                depth = i;
                position = from;
            }
            if to % 2 == 0  && self.vec[i][to] < self.vec[depth][position] {
                depth = i;
                position = to;
            }
            if to == 0 {
                return self.vec[depth][position]
            }else {
                from = (from + 1) / 2;
                to = (to - 1) / 2;
                if to < from {
                    return self.vec[depth][position]
                }
            }
        }
        self.vec[depth][position]
    }
    fn renew(&mut self, mut position: usize, value: T){
        self.vec[0][position] = value;
        for i in 1 .. self.vec.len() {
            position /= 2;
            if position * 2 + 1 < self.vec[i - 1].len() {
                let new_value = min(self.vec[i - 1][position * 2], self.vec[i - 1][position * 2 + 1]);
                if self.vec[i][position] != new_value {
                    self.vec[i][position] = new_value;
                } else {
                    break
                }
            }else {
                break
            }
        }
    }
}
fn main(){
    let n: usize = read_value!();
    let mut words = Vec::<String>::with_capacity(n);
    let mut lexical_order_to_appear_order = Vec::with_capacity(n);
    let mut appear_order_to_lexical_order = Vec::with_capacity(n);
    let mut is_prefix_until = Vec::with_capacity(n);
    for i in 0 .. n {
        words.push(read_value!());
        lexical_order_to_appear_order.push(i);
        appear_order_to_lexical_order.push(i);
        is_prefix_until.push(i);
    }
    lexical_order_to_appear_order.sort_by_key(|&i| &words[i]);
    appear_order_to_lexical_order.sort_by_key(|&i| lexical_order_to_appear_order[i]);
    let words = words;
    let lexical_order_to_appear_order = lexical_order_to_appear_order;
    let appear_order_to_lexical_order = appear_order_to_lexical_order;
    for i in 0 .. is_prefix_until.len() {
        is_prefix_until[i] = (i .. lexical_order_to_appear_order.len()).first_true(|p| !words[lexical_order_to_appear_order[p]].starts_with(&words[lexical_order_to_appear_order[i]])).unwrap_or(n);
    }
    let mut using_by_lexical_order = MinSegmentTree::create_from(&vec![n; n]);
    let un_use = n;
    let q: usize = read_value!();
    for _ in 0 .. q {
        let_all!(k: usize, word_id: usize);
        match k {
            1 => {
                using_by_lexical_order.renew(appear_order_to_lexical_order[word_id - 1], appear_order_to_lexical_order[word_id - 1]);
            }
            2 => {
                using_by_lexical_order.renew(appear_order_to_lexical_order[word_id - 1], un_use);
            }
            3 => {
                let next = using_by_lexical_order.min_of(appear_order_to_lexical_order[word_id - 1], n);
                if next == un_use || next >= is_prefix_until[appear_order_to_lexical_order[word_id - 1]]{
                    println!("-1");
                }else {
                    println!("{}", lexical_order_to_appear_order[next] + 1);
                }
            }
            _ => unreachable!()
        }
    }
}


Measured memory_bytes: 15292