#include <bits/stdc++.h>
using namespace std;
#define MAX 19
int a[200010];
pair <int, pair<int, int> > b[200010];
pair <int, int> d[MAX];
pair <int, int> c[200010];
int block;
int resenje;
bool dalije (int n) {
    for (int i = 1; i <= n; i++) {
        if (a[i] != 1 && a[i] != 0)
            return false;
    }
    return true;
}

bool poredjenje (pair <int, pair <int, int> > x, pair <int, pair<int, int> > y) {
    if (x.first / block < y.first / block)
        return true;
    else if (x.first / block > y.first / block)
        return false;
    else
        return x.second.first / block < y.second.first / block;
}

bool poredjenje2 (pair <int, int>x, pair <int, int>y) {
    if (x.second < y.second)
        return true;
    else
        return false;
}
void dekadnoubinarno (bool a[], int x) {
    for (int i = MAX; i >= 0; i--) {
        int y = pow(2, i);
        if (x - y >= 0) {
            a[i] = true;
            x -= y;
        }
        else
            a[i] = false;
    }
}

void dodaj(bool a[], pair <int, int> d[]) {
    for (int i = 0; i <= MAX; i++) {
        if (a[i])
            d[i].first++;
        else
            d[i].second++;
    }
}
void oduzmi (bool a[], pair<int, int>d[]) {
    for (int i = 0; i <= MAX; i++) {
        if (a[i])
            d[i].first--;
        else
            d[i].second--;
    }
}

void ispisib (int q) {
    for (int i = 0; i < q; i++) {
        cout << b[i].first << b[i].second.first << b[i].second.second << endl;
    }
}
void ispisid () {
    for (int i = 0; i < MAX; i++) {
        cout << d[i].first << " " << d[i].second << endl;
    }
}

void resimizadatak (int n, int q) {
    block = sqrt(n);
    bool r1[19];
    sort (b, b + q, poredjenje);
    resenje = a[b[0].first];
    for (int j = b[0].first + 1; j <= b[0].second.first; j++) {
        dekadnoubinarno(r1, a[j]);
        dodaj(r1, d);
        resenje = resenje & a[j];
    }
    c[0].first = resenje;
    c[0].second = b[0].second.second;
    for (int i = 1; i < q; i++) {
        ispisid();
        resenje = 0;
        if (b[i].first < b[i - 1].first)
            for (int j = b[i].first; j < b[i - 1].first; j++) {
                dekadnoubinarno(r1, a[j]);
                dodaj (r1, d);
            }
        else
            for (int j = b[i - 1].first; j <= b[i].first - 1 && j <= b[i - 1].second.first; j++) {
                dekadnoubinarno(r1, a[j]);
                oduzmi (r1, d);
            }
        if (b[i].second.first > b[i - 1].second.first)
            for (int j = b[i - 1].second.first + 1; j <= b[i].second.first; j++) {
                dekadnoubinarno(r1, a[j]);
                dodaj(r1, d);
            }
        else
            for (int j = b[i].second.first + 1; j <= b[i - 1].second.first; j++) {
                dekadnoubinarno(r1, a[j]);
                oduzmi(r1, d);
            }
        for (int i = 0; i <= MAX; i++) {
            if (d[i].first > 0 && d[i].second < 0)
                resenje += pow(2, i);
        }
        c[i].first = resenje;
        c[i].second = b[i].second.second;
    }
}