Help me in solving LEXILARGEST problem

My issue

What exactly is the motivation for subtracting a[i] from b[i] incase gcd(b[i],a[i-1])!=a[i]?

My code

from math import gcd
for t in range(int(input())):
    n,m=list(map(int,input().split()))
    a=[*map(int,input().split())]
    b=[a[0]]+[0]*(n-1)
    for i in range(1,n):
        b[i]=(m//a[i])*a[i]
        while gcd(b[i],a[i-1])!=a[i]:b[i]-=a[i]
    print(*b)

Problem Link: Lexicographically Largest Practice Coding Problem - CodeChef