Under construction

Still being built. Things may change

Palindrome

Time limit2sMemory limit512 MB

Summary
Given a string s, each query concatenates the length-l substrings starting at the listed positions and asks for the number of palindromic substrings of the result.
Level

Hard9 of 10

Topics
String, String matching, Math
Solved
No attempts yet

Problem

A palindromic string is a string that reads the same forward and backward.

Yuuka has a string ss and wants to ask some questions about it. In each question, Yuuka gives a list of integers x1,x2,…,xkx_1, x_2, \dots, x_k (where xix_i is 1-indexed) and an integer ll. Let s(x,p)s(x, p) denote the substring of ss of length pp that starts at position xx. Let tt be the concatenation of s(x1,l),s(x2,l),…,s(xk,l)s(x_1, l), s(x_2, l), \dots, s(x_k, l). Yuuka wants to know the total number of palindromic substrings of tt.

Input

The input contains zero or more test cases and ends at end-of-file. Each test case is as follows.

The first line contains the length nn of the string (1≤n≤1051 \le n \le 10^5).

The second line contains nn integers s1,s2,…,sns_1, s_2, \dots, s_n that denote the string ss (1≤si≤n1 \le s_i \le n). Yuuka's alphabet may be large, so each character is written as an integer from 1 to nn.

The third line contains the number of questions mm (1≤m≤1051 \le m \le 10^5).

Each of the next mm lines contains two integers kik_i and lil_i, followed by kik_i integers xi,1,xi,2,…,xi,kix_{i,1}, x_{i,2}, \dots, x_{i,k_i} (1≤ki≤1051 \le k_i \le 10^5, 1≤li≤n1 \le l_i \le n, ki⋅li≤109k_i \cdot l_i \le 10^9, 1≤xi,j≤n−li+11 \le x_{i,j} \le n - l_i + 1).

The sum of all nn and the sum of all kik_i do not exceed 10510^5.

Output

For each question, output the number of palindromic substrings of tt as an integer.

Examples1

  1. Example 1

    Input
    9
    1 2 1 2 1 2 1 2 1
    3
    3 3 1 4 7
    9 1 1 2 3 4 5 6 7 8 9
    4 3 1 2 1 2
    
    Expected output
    25
    25
    42