Language: C++

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

Compiler output:
a.cc:1:5: error: 'import' does not name a type
    1 |     import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
      |     ^~~~~~
a.cc:1:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:2:5: error: 'import' does not name a type
    2 |     import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
      |     ^~~~~~
a.cc:2:5: note: C++20 'import' only available with '-fmodules-ts'
a.cc:5:5: error: '::main' must return 'int'
    5 |     void main() {
      |     ^~~~
a.cc: In function 'int main()':
a.cc:7:9: error: 'scan' was not declared in this scope
    7 |         scan(n, q);
      |         ^~~~
a.cc:12:13: error: 'solve' was not declared in this scope
   12 |             solve(n, vi, wi);
      |             ^~~~~
a.cc: In function 'void solve(int, int, int)':
a.cc:18:21: error: 'min' was not declared in this scope; did you mean 'main'?
   18 |             writeln(min(v, w));
      |                     ^~~
      |                     main
a.cc:18:13: error: 'writeln' was not declared in this scope
   18 |             writeln(min(v, w));
      |             ^~~~~~~
a.cc:22:30: error: parenthesized initializer in array new
   22 |         auto va = new int[](0);
      |                              ^
a.cc:23:11: error: expected ';' before '~' token
   23 |         va ~= v;
      |           ^~
      |           ;
a.cc:27:15: error: expected ';' before '~' token
   27 |             va ~= v;
      |               ^~
      |               ;
a.cc:30:30: error: parenthesized initializer in array new
   30 |         auto wa = new int[](0);
      |                              ^
a.cc:31:11: error: expected ';' before '~' token
   31 |         wa ~= w;
      |           ^~
      |           ;
a.cc:35:15: error: expected ';' before '~' token
   35 |             wa ~= w;
      |               ^~
      |               ;
a.cc:38:9: error: 'debug' was not declared in this scope
   38 |         debug {
      |         ^~~~~
a.cc:45:18: error: 'vi' was not declared in this scope; did you mean 'v'?
   45 |         foreach (vi ; va) {
      |                  ^~
      |                  v
a.cc:45:25: error: expected ';' before ')' token
   45 |         foreach (vi ; va) {
      |                         ^
      |                         ;
a.cc:53:9: error: 'writeln' was not declared in this scope
   53 |         writeln(ans);
      |         ^~~~~~~
a.cc: At global scope:
a.cc:58:10: error: variable or field 'scan' declared void
   58 |     void scan(T...)(ref T args) {
      |          ^~~~
a.cc:58:15: error: 'T' was not declared in this scope
   58 |     void scan(T...)(ref T args) {
      |               ^
a.cc:68:10: error: variable or field 'fillAll' declared void
   68 |     void fillAll(R, T)(ref R arr, T value) {
      |          ^~~~~~~
a.cc:68:18: error: 'R' was not declared in this scope
   68 |     void fillAll(R, T)(ref R arr, T value) {
      |                  ^
a.cc:68:21: error: 'T' was not declared in this scope
   68 |     void fillAll(R, T)(ref R arr, T value) {
      |                     ^