mod=998244353 def solve(): n,x=map(int,input().split()) N=n if x%2==1: lose=n//2 print((n-lose)%mod) return lose=(n//(2*(x+1)))*x #print(lose) n%=(2*(x+1)) lose+=min(x//2,n//2+1) #print(lose) n-=x+1 lose+=max(0,min(x//2,n//2+1)) #print(lose) ans=N+1-lose print(ans%mod) for _ in range(int(input())): solve()