mod = 1000000007 eps = 10**-9 def main(): import sys from itertools import permutations input = sys.stdin.readline def guchoku(N, M): res = float("inf") for P in permutations(list(range(1, N+1))): A = list(range(1, N+1)) ans = 0 for p in P[:-1]: tmp = M for a in A: if a == p: continue tmp = min(tmp, (a + p) % M) ans += tmp A.remove(p) res = min(res, ans) return res for _ in range(int(input())): N, M = map(int, input().split()) if M == 1: print(0) continue if M <= N: if M == 2: print(1) else: print(M // 2) elif M >= 2 * N: print(N - 1 + (N * (N + 1)) // 2 - 1) else: K = M - N - 1 if K == 0: print((N - 1) // 2) else: print((N - K - 1) // 2 + K + ((K + 1) * (K + 2)) // 2 - 1) #print(guchoku(N, M)) if __name__ == '__main__': main()