Under construction

Still being built. Things may change

Calendars

Interview

Time limit1sMemory limit512 MB

Summary
Convert each given date to its day of year with one calendar, then locate the matching month and day in the other calendar.
Level

Medium4 of 10

Topics
Prefix sum, Binary search
Solved
No attempts yet

Problem

You manage trade between two tribes, the Arbuzans and the Bananits. The trouble is that the two tribes use different calendars.

The Arbuzan calendar has nn months whose lengths, in days, are a1,a2,…,ana_1, a_2, \dots, a_n. The Bananit calendar has mm months whose lengths are b1,b2,…,bmb_1, b_2, \dots, b_m.

A year has the same number of days in both calendars, that is a1+a2+⋯+an=b1+b2+⋯+bma_1 + a_2 + \dots + a_n = b_1 + b_2 + \dots + b_m.

Write a program that converts dates between the Arbuzan and the Bananit calendars.

Input

The first line contains two integers nn and mm (1≤n,m≤1061 \le n, m \le 10^6), separated by a single space, the number of months in the Arbuzan and the Bananit calendars.

The second line contains the integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1031 \le a_i \le 10^3), separated by single spaces, the lengths of the consecutive months of the Arbuzan calendar. The third line contains the integers b1,b2,…,bmb_1, b_2, \dots, b_m (1≤bi≤1031 \le b_i \le 10^3), separated by single spaces, the lengths of the consecutive months of the Bananit calendar.

The fourth line contains a single integer zz (1≤z≤1051 \le z \le 10^5), the number of queries.

Each of the next zz lines describes one query. It consists of two integers did_i and mim_i and one character cic_i, separated by single spaces, giving the day, the month, and the direction of conversion. If cic_i is the letter A, then 1≤mi≤n1 \le m_i \le n and 1≤di≤ami1 \le d_i \le a_{m_i}, and the pair denotes a date in the Arbuzan calendar that your program must convert to the Bananit calendar. If cic_i is the letter B, then 1≤mi≤m1 \le m_i \le m and 1≤di≤bmi1 \le d_i \le b_{m_i}, and the pair denotes a date in the Bananit calendar that your program must convert to the Arbuzan calendar.

Output

Print zz lines. The ii-th line must contain the answer to the ii-th query as two integers di′d^{\prime}_i and mi′m^{\prime}_i separated by a single space, where di′d^{\prime}_i is the day and mi′m^{\prime}_i is the month of the converted date in the target calendar.

Examples2

  1. Example 1

    Input
    3 6
    20 10 4
    10 10 6 4 2 2
    4
    11 1 A
    2 1 B
    2 6 B
    3 3 A
    
    Expected output
    1 2
    2 1
    4 3
    1 6
    
  2. Example 2

    Input
    1 1
    5
    5
    3
    1 1 A
    5 1 A
    3 1 B
    
    Expected output
    1 1
    5 1
    3 1