Under construction

Still being built. Things may change

Determining Nucleotide Assortments

Interview

Time limit1sMemory limit1024 MB

Summary
Given a DNA string and many range queries, output the four nucleotides ordered by count in each range, with ties broken by ATGC order.
Level

Easy3 of 10

Topics
Prefix sum, Sorting, String
Solved
No attempts yet

Problem

Genes 'R Us analyzes DNA strands to find anomalies, matches, patterns, or other specific items its customers want. You may recall from high school biology that a DNA molecule consists of two chains wrapped around each other to form the double helix. Each chain is made of four types of nucleotides, distinguished by their nitrogen bases: adenine (A), thymine (T), guanine (G), and cytosine (C).

A recent customer of Genes 'R Us wants to quickly determine which of the four nucleotide types is most prevalent in a given section of DNA, and which are second, third, and fourth most prevalent. A strand may contain tens of thousands of nucleotides, and the customer will ask about many sections of the same strand. Your task is to write an efficient program that answers these queries.

Input

The first line is a string describing a DNA strand. It consists only of the characters A, T, G, and C, and its length nn satisfies n≤50 000n \leq 50\,000. The next line contains a positive integer mm (m≤25 000m \leq 25\,000), the number of sections to investigate. The following mm lines each contain a pair of integers sis_i and eie_i, the start and end positions of a section, where 1≤si≤ei≤n1 \leq s_i \leq e_i \leq n. The two integers in each pair are separated by a single space.

Output

For each section, output one line containing a permutation of ATGC. The first character is the most prevalent nucleotide in the section, the second character is the second most prevalent, and so on. If nucleotides occur equally often, order them by ATGC: A comes before T, G, and C, and T comes before G and C.

Examples1

  1. Example 1

    Input
    TATATGCTCT
    3
    1 10
    6 10
    6 6
    
    Expected output
    TACG
    TCGA
    GATC