import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;


void main() {
    int n, q;
    scan(n, q);

    while (q--) {
        int vi, wi;
        scan(vi, wi);
        solve(n, vi, wi);
    }
}

void solve(int n, int v, int w) {
    if (n == 1) {
        writeln(min(v, w));
        return;
    }

    auto va = new int[](0);
    va ~= v;

    while (v > 1) {
        if (v % n <= 1) {
            v /= n;
        }
        else {
            v = (v / n) + 1;
        }

        va ~= v;
    }

    va.reverse();

    auto wa = new int[](0);
    wa ~= w;

    while (w > 1) {
        if (w % n <= 1) {
            w /= n;
        }
        else {
            w = (w / n) + 1;
        }

        wa ~= w;
    }

    wa.reverse();

    debug {
        writeln(va);
        writeln(wa);
    }

    int ans = 0;

    while (!va.empty) {
        while (!wa.empty) {
            if (va.front == wa.front) {
                ans = max(ans, va.front);
                va.popFront;
                wa.popFront;
                break;
            }
            else if (va.front < wa.front) {
                va.popFront;
                break;
            }
            else {
                wa.popFront;
            }
        }
    }

    writeln(ans);
}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}