import sys input = sys.stdin.readline import random from collections import * def grundy(c, t, f1, f2): # if memo[c][t][f1][f2]!=-1: # return memo[c][t][f1][f2] if c==0: return 0 s = set() if t==0: s.add(grundy(c-1, 1^t, 1, f2)) if c-X>=0 and f1==1: s.add(grundy(c-X, 1^t, 0, f2)) else: s.add(grundy(c-1, 1^t, f1, 1)) if c-X>=0 and f2==1: s.add(grundy(c-X, 1^t, f1, 0)) res = 0 while res in s: res += 1 #memo[c][t][f1][f2] = res return res # for X in range(100): # print('X:', X, '|', end=' ') # memo = [[[[-1]*2 for _ in range(2)] for _ in range(2)] for _ in range(110)] # acc = 0 # for N in range(100): # g = grundy(N, 0, 1, 1) # # print(1 if g>=1 else 0, end=' ') # if X%2==0: # if g>=1: # acc += 1 # print(acc, end=' ') # print() def f(N): res = 0 for i in range(1, N+1): if grundy(i, 0, 1, 1)>=1: res += 1 return res MOD = 998244353 for _ in range(int(input())): N, X = map(int, input().split()) # N, X = random.randint(1, 100), random.randint(1, 100) # print('N X', N, X) if X%2==1: ans = (N-N//2)%MOD # print(ans, f(N)) print(ans) else: s = X//2 d = 2+X//2 if (N+1)//2