TorCoder

A boy named Leo doesn’t miss a single TorCoder contest round. On the last TorCoder round number 100666 Leo stumbled over the following problem. He was given a string s, consisting of n lowercase English letters, and m queries. Each query is characterised by a pair of integers l i, r i (1 ≤ l i ≤ r i ≤ n).

We’ll consider the letters in the string numbered from 1 to n from left to right, that is, s = s 1 s 2… s n.

After each query he must swap letters with indexes from l i to r i inclusive in string s so as to make substring (l i, r i) a palindrome. If there are multiple such letter permutations, you should choose the one where string (l i, r i) will be lexicographically minimum. If no such permutation exists, you should ignore the query (that is, not change string s).

Everybody knows that on TorCoder rounds input line and array size limits never exceed 60, so Leo solved this problem easily. Your task is to solve the problem on a little bit larger limits. Given string s and m queries, print the string that results after applying all m queries to string s.

Input

The first input line contains two integers n and m (1 ≤ n, m ≤ 105) — the string length and the number of the queries.

The second line contains string s, consisting of n lowercase Latin letters.

Each of the next m lines contains a pair of integers l i, r i (1 ≤ l i ≤ r i ≤ n) — a query to apply to the string.

Output

In a single line print the result of applying m queries to string s. Print the queries in the order in which they are given in the input.

Examples

input

7 2
aabcbaa
1 3
5 7

output

abacaba

input

3 2
abc
1 2
2 3

output

abc

Note

substring (l i, r i) 1 ≤ l i ≤ r i ≤ n) of string s = s 1 s 2… s n of length n is a sequence of characters s l i s l i + 1s r i.

A string is a palindrome, if it reads the same from left to right and from right to left.

String x 1 x 2… x p is lexicographically smaller than string y 1 y 2… y q, if either p < q and x 1 = y 1, x 2 = y 2, … , x p = y p, or exists such number r (r < p, r < q), that x 1 = y 1, x 2 = y 2, … , x r = y r and x r + 1 < y r + 1.

Solution:

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>

using namespace std;

const int N = 400010;

int s[28][N];
char v[28][N], p[28][N];
int *ss;
char *vv, *pp;

void modify(int q, int x, int l, int r, int ll, int rr, int t) {
if (l >= ll && r <= rr) {
    vv[x] = t;
    pp[x] = 1;
    ss[x] = t*(r-l+1);
    return;
  }
  if (pp[x]) {
    vv[x+x] = vv[x+x+1] = vv[x];
    pp[x+x] = pp[x+x+1] = 1;
    if (!vv[x]) ss[x+x] = ss[x+x+1] = 0; else {
      ss[x+x] = (((l+r) >> 1)-l+1);
ss[x+x+1] = (r-((l+r) >> 1));
}
pp[x] = 0;
}
int y = (l+r) >> 1;
if (ll <= y) modify(q, x+x, l, y, ll, rr, t);
  if (rr > y) modify(q, x+x+1, y+1, r, ll, rr, t);
ss[x] = ss[x+x] + ss[x+x+1];
}

int findsum(int q, int x, int l, int r, int ll, int rr) {
if (l >= ll && r <= rr) return ss[x];
  if (pp[x]) {
    vv[x+x] = vv[x+x+1] = vv[x];
    pp[x+x] = pp[x+x+1] = 1;
    if (!vv[x]) ss[x+x] = ss[x+x+1] = 0; else {
      ss[x+x] = (((l+r) >> 1)-l+1);
ss[x+x+1] = (r-((l+r) >> 1));
}
pp[x] = 0;
}
int y = (l+r) >> 1, res = 0;
if (ll <= y) res += findsum(q, x+x, l, y, ll, rr);
  if (rr > y) res += findsum(q, x+x+1, y+1, r, ll, rr);
ss[x] = ss[x+x] + ss[x+x+1];
return res;
}

int n, m, i, lo, hi, cnt[255];
char c, st[N];

int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d %d", &n, &m);
scanf("%s", st);
for (i=0;i<n;i++) {
    pp = p[st[i]-'a'];
    vv = v[st[i]-'a'];
    ss = s[st[i]-'a'];
    modify(st[i]-'a', 1, 0, n-1, i, i, 1);
  }
  while (m--) {
    scanf("%d %d", &lo, &hi);
    lo--; hi--;
    char bad = ' ';
    for (c='a';c<='z';c++) {
      pp = p[c-'a'];
      vv = v[c-'a'];
      ss = s[c-'a'];
      cnt = findsum(c-'a', 1, 0, n-1, lo, hi);
      if (cnt & 1)
        if (bad == ' ') bad = c; else {
          bad = '!';
          break;
        }
    }
    if (bad == '!') continue;
    int oldlo = lo, oldhi = hi;
    for (c='a';c<='z';c++)
      if (cnt > 0) {
pp = p[c-'a'];
vv = v[c-'a'];
ss = s[c-'a'];
modify(c-'a', 1, 0, n-1, oldlo, oldhi, 0);
int u = cnt/2;
if (!u) continue;
modify(c-'a', 1, 0, n-1, lo, lo+u-1, 1);
lo += u;
modify(c-'a', 1, 0, n-1, hi-u+1, hi, 1);
hi -= u;
}
if (bad != ' ') {
pp = p[bad-'a'];
vv = v[bad-'a'];
ss = s[bad-'a'];
modify(bad-'a', 1, 0, n-1, lo, hi, 1);
}
}
for (i=0;i<n;i++) st[i] = 0;
  for (c='a';c<='z';c++) {
    pp = p[c-'a'];
    vv = v[c-'a'];
    ss = s[c-'a'];
    for (i=0;i<n;i++)
      if (!st[i] && findsum(c-'a', 1, 0, n-1, i, i) == 1) st[i] = c;
  }
  for (i=0;i<n;i++) putchar(st[i]);
  printf("\n");
  return 0;
}