void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int n, q; rd(n, q);
  while(q--){
    int v, w; rd(v, w);
    v--; w--;
    if(n==1){writeln(v); continue;}
    auto fun(int i){
      int ret=0;
      while(i>0) i=(i-1)/n, ret++;
      return ret;
    }
    auto dv=fun(v), dw=fun(w);
    foreach(_; 0..(dw-dv)) w=(w-1)/n;
    if(v==w){writeln(v+1); continue;}
    while(v!=w){
      v=(v-1)/n;
      w=(w-1)/n;
    }
    writeln(v+1);
  }
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
void wr(T...)(T x){
  import std.stdio;
  foreach(e; x) write(e, " ");
  writeln();
}