Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
from itertools import accumulate


def solve(v, w):
    vl = bisect(table, v)
    wl = bisect(table, w)
    if vl > wl:
        for i in range(vl - wl):
            v = (v + m) // n
    elif wl < vl:
        for i in range(wl - vl):
            w = (w + m) // n
    while v != w:
        if v > w:
            v, w = w, v
        w = (w + m) // n
    return v


n, q = map(int, input().split())
m = n - 2
table, k = [], 1
while True:
    table.append(k)
    k *= n
    if k > 1e9:
        break
table = list(accumulate(table))

print('\n'.join(map(str, (solve(*map(int, input().split())) for _ in range(q)))))


Fixed submission:
n, q = map(int, input().split())

if n == 1:
    def solve(v, w):
        return min(v, w)
else:
    def solve(v, w):
        while v != w:
            if v > w:
                v, w = w, v
            w = (w + n - 2) // n
        return v

print('\n'.join(map(str, (solve(*map(int, input().split())) for _ in range(q)))))

Buggy python submission (verdict: Runtime Error):
from bisect import bisect
from itertools import accumulate


def solve(v, w):
    vl = bisect(table, v)
    wl = bisect(table, w)
    if vl > wl:
        for i in range(vl - wl):
            v = (v + m) // n
    elif wl < vl:
        for i in range(wl - vl):
            w = (w + m) // n
    while v != w:
        if v > w:
            v, w = w, v
        w = (w + m) // n
    return v


n, q = map(int, input().split())
m = n - 2
table, k = [], 1
while True:
    table.append(k)
    k *= n
    if k > 1e9:
        break
table = accumulate(table)

print('\n'.join(map(str, (solve(*map(int, input().split())) for _ in range(q)))))


Fixed submission:
n, q = map(int, input().split())

if n == 1:
    def solve(v, w):
        return min(v, w)
else:
    def solve(v, w):
        while v != w:
            if v > w:
                v, w = w, v
            w = (w + n - 2) // n
        return v

print('\n'.join(map(str, (solve(*map(int, input().split())) for _ in range(q)))))

Buggy python submission (verdict: Time Limit Exceeded):
def solve(v, w):
    while v != w:
        if v > w:
            v, w = w, v
        w = (w + n - 2) // n
    return v


n, q = map(int, input().split())
print('\n'.join(map(str, (solve(*map(int, input().split())) for _ in range(q)))))


Fixed submission:
n, q = map(int, input().split())

if n == 1:
    def solve(v, w):
        return min(v, w)
else:
    def solve(v, w):
        while v != w:
            if v > w:
                v, w = w, v
            w = (w + n - 2) // n
        return v

print('\n'.join(map(str, (solve(*map(int, input().split())) for _ in range(q)))))

Buggy python submission (verdict: Time Limit Exceeded):
def solve(v, w):
    while v != w:
        if v > w:
            v, w = w, v
        w = (w + n - 2) // n
    return v


n, q = map(int, input().split())
print('\n'.join(map(str, (solve(*map(int, input().split())) for _ in range(q)))))


Fixed submission:
n, q = map(int, input().split())

if n == 1:
    def solve(v, w):
        return min(v, w)
else:
    def solve(v, w):
        while v != w:
            if v > w:
                v, w = w, v
            w = (w + n - 2) // n
        return v

print('\n'.join(map(str, (solve(*map(int, input().split())) for _ in range(q)))))