gets.to_i.times{ n,x=gets.split.map &:to_i ans=0 n+=1 if x%2==1 ans+=n/2 else t=x*(x+1) ans+=n/t*(x/2*(x+2)) n%=t if n<=x ans+=n/2 else n-=x ans+=x/2 if n<=x ans+=(n+1)/2 else n-=x ans+=x/2 t=n/x ans+=2*t ans+=(x/2-2)*t ans+=(t+1)/2 n%=x if n<=t+1 if t%2==0 ans+=(n+1)/2 else ans+=n/2 end else ans+=(t+2)/2 n-=t+1 ans+=(n+1)/2 end end end end p ans%998244353 }