Under construction

Still being built. Things may change

Two Sequences

Time limit3sMemory limit1024 MB

Summary
For each query, find the k-th element of the lexicographically smallest merge of the prefixes A[1..a] and B[1..b].
Level

Hard9 of 10

Topics
String, Greedy, Segment tree
Solved
No attempts yet

Problem

Two sequences X1,⋯ ,XsX_1, \cdots, X_s and Y1,⋯ ,YtY_1, \cdots, Y_t are given. Starting from an empty sequence SS, perform a move exactly s+ts+t times. Define X⋆YX \star Y as the lexicographically smallest sequence that can be the final state of SS.

A move is one of the following two actions.

  • If XX is not empty, remove the first element of XX and append it to the end of SS.
  • If YY is not empty, remove the first element of YY and append it to the end of SS.

You are given an integer sequence A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N of length NN and an integer sequence B1,B2,⋯ ,BMB_1, B_2, \cdots, B_M of length MM.

Write a program that processes the following query.

  • a b k: Print the kk-th number of A[1..a]⋆B[1..b]A[1..a] \star B[1..b].

Input

The first line contains NN, MM, and QQ, separated by spaces. (1≤N,M,Q≤100 0001\le N,M,Q\le 100\,000)

The second line contains A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N, separated by spaces. (1≤Ai≤1091\le A_i\le 10^9)

The third line contains B1,B2,⋯ ,BMB_1, B_2, \cdots, B_M, separated by spaces. (1≤Bi≤1091\le B_i\le 10^9)

Each of the next QQ lines contains a query aa, bb, and kk, separated by spaces. (1≤a≤N1\le a\le N; 1≤b≤M1\le b\le M; 1≤k≤a+b1\le k\le a+b)

Output

Print QQ lines. Print the answer to each query in the order the queries are given.

Examples1

  1. Example 1

    Input
    5 4 9
    2 4 2 5 1
    2 4 1 3
    1 1 1
    5 4 3
    2 3 3
    5 1 6
    1 1 2
    5 1 3
    3 2 3
    3 3 3
    4 2 5
    
    Expected output
    2
    4
    4
    1
    2
    4
    4
    4
    4