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) {
        v = (v + n - 2) / n;
        va ~= v;
    }

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

    while (w > 1) {
        w = (w + n - 2) / n;
        wa ~= w;
    }

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

    int ans = 0;

    foreach (vi ; va) {
        foreach (wi ; wa) {
            if (vi == wi) {
                ans = max(ans, vi);
            }
        }
    }

    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);
        }
    }
}