mod = 998244353 t = int(input()) for i in range(t): n,x = map(int,input().split()) if n < x or x % 2 == 1: print(((n+1)//2) % mod) else: ans = x//2 + (n-x+1)//(x+3) * ((x+4)//2) + (n-x+1)%(x+3)//2 + 1 ans %= mod print(ans)