One Trick Access

3

1 votes
Loops, Heavy light decomposition, Hard, Tree, Data Structures
Problem

You are given N strings, namely S1,S2,,SN.

M other strings will be constructed as a concatenation of other strings. More specifically, the ith (N+1iN+M) string Si will be constructed as a concatenation of several other strings Sk, where 1ki1.

You must answer Q queries consisting of an index i (1iN+M) and an integer p (1pmin(|Si|,250); you must print the pth character of Si.

Input

The first line contains two integer N and M.

The next N+M lines describe S1,S2,,SN+M.

The first N lines consist of actual strings S1,S2,...,SN.

The next M lines describe the construction of SN+1,SN+2,,SN+M. Every line i (1iM) consists of an integer ki and ki indices qi1,qi2,,qiki, meaning that Si+N=Sqi1+Sqi2++Sqiki, where + is the string concatenation operator.

The next line contains an integer Q.

Each of the following Q lines consist of two integers i and p.

Output

Print a string of Q characters, in which the ith character represents the answer for the ith query.

Notes

  • 1N,M,Q105
  • 1|Si| for 1iN+M
  • Ni=1|Si|106
  • Mi=1ki106
  • 1qiti+N1 for 1iM and 1tki
  • All strings consist of lowercase English characters
Time Limit: 2
Memory Limit: 512
Source Limit:
Explanation

S6="abcdddqxxasfqscedv" and S7="qscedvddd"

In the first query we must print the 3rdcharacter of S1.

Editor Image

?