package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
buf := make([]byte, 10*1024*1024)
scanner.Buffer(buf, 10*1024*1024)
scanner.Split(bufio.ScanWords)
if !scanner.Scan() {
return
}
n, _ := strconv.Atoi(scanner.Text())
str_head := make([]int32, n+5)
str_next := make([]int32, n+5)
str_char := make([]byte, n+5)
for i := 1; i <= n; i++ {
scanner.Scan()
typ, _ := strconv.Atoi(scanner.Text())
if typ == 1 {
scanner.Scan()
c := scanner.Text()[0]
str_next[i] = str_head[0]
str_head[0] = int32(i)
str_char[i] = c - 'a'
} else {
scanner.Scan()
j, _ := strconv.Atoi(scanner.Text())
scanner.Scan()
c := scanner.Text()[0]
str_next[i] = str_head[j]
str_head[j] = int32(i)
str_char[i] = c - 'a'
}
}
scanner.Scan()
m, _ := strconv.Atoi(scanner.Text())
queries_head := make([]int32, n+5)
queries_next := make([]int32, m+5)
query_state := make([]int32, m+5)
ans := make([]int32, m+5)
ch := make([][26]int32, 400005)
var tot int32 = 0
insert := func(s string) int32 {
var u int32 = 0
for i := 0; i < len(s); i++ {
c := s[i] - 'a'
if ch[u][c] == 0 {
tot++
if int(tot) >= len(ch) {
ch = append(ch, [26]int32{})
}
ch[u][c] = tot
}
u = ch[u][c]
}
return u
}
for i := 1; i <= m; i++ {
scanner.Scan()
idx, _ := strconv.Atoi(scanner.Text())
scanner.Scan()
t := scanner.Text()
queries_next[i] = queries_head[idx]
queries_head[idx] = int32(i)
query_state[i] = insert(t)
}
fail := make([]int32, tot+5)
q := make([]int32, 0, tot+5)
for c := 0; c < 26; c++ {
if ch[0][c] != 0 {
q = append(q, ch[0][c])
}
}
for head := 0; head < len(q); head++ {
u := q[head]
for c := 0; c < 26; c++ {
if ch[u][c] != 0 {
fail[ch[u][c]] = ch[fail[u]][c]
q = append(q, ch[u][c])
} else {
ch[u][c] = ch[fail[u]][c]
}
}
}
fail_head := make([]int32, tot+5)
fail_next := make([]int32, tot+5)
for i := int32(1); i <= tot; i++ {
fail_next[i] = fail_head[fail[i]]
fail_head[fail[i]] = i
}
in := make([]int32, tot+5)
out := make([]int32, tot+5)
var timer int32 = 0
type StackFrame struct {
u int32
edge int32
}
stack := make([]StackFrame, 0, tot+5)
stack = append(stack, StackFrame{0, fail_head[0]})
timer++
in[0] = timer
for len(stack) > 0 {
top := len(stack) - 1
u := stack[top].u
e := stack[top].edge
if e != 0 {
stack[top].edge = fail_next[e]
timer++
in[e] = timer
stack = append(stack, StackFrame{e, fail_head[e]})
} else {
out[u] = timer
stack = stack[:top]
}
}
bit := make([]int32, tot+5)
add := func(idx, val int32) {
for ; idx <= tot+2; idx += idx & -idx {
bit[idx] += val
}
}
query := func(idx int32) int32 {
var sum int32 = 0
for ; idx > 0; idx -= idx & -idx {
sum += bit[idx]
}
return sum
}
type StrStackFrame struct {
u int32
ac_state int32
edge int32
added bool
}
str_stack := make([]StrStackFrame, 0, n+5)
str_stack = append(str_stack, StrStackFrame{0, 0, str_head[0], false})
for len(str_stack) > 0 {
top := len(str_stack) - 1
u := str_stack[top].u
ac_state := str_stack[top].ac_state
e := str_stack[top].edge
if !str_stack[top].added {
if u != 0 {
add(in[ac_state], 1)
}
for qi := queries_head[u]; qi != 0; qi = queries_next[qi] {
st := query_state[qi]
ans[qi] = query(out[st]) - query(in[st]-1)
}
str_stack[top].added = true
}
if e != 0 {
str_stack[top].edge = str_next[e]
nxt_state := ch[ac_state][str_char[e]]
str_stack = append(str_stack, StrStackFrame{e, nxt_state, str_head[e], false})
} else {
if u != 0 {
add(in[ac_state], -1)
}
str_stack = str_stack[:top]
}
}
out_writer := bufio.NewWriter(os.Stdout)
for i := 1; i <= m; i++ {
fmt.Fprintln(out_writer, ans[i])
}
out_writer.Flush()
}