from sys import stdin
input=stdin.readline
t=int(input())
for i in range(t):
    n,m=map(int,input().split())
    nd=n//2
    if(m==0):
        print(0)
        continue
    if(n==1):
        print(1)
        continue
    else:
        if(n%2==1):
            if(m<=nd):
                k=((n+1)/2)**2+(m-1)*(n-2)
                print(int(k))
            else:
                k=((n+1)/2)**2+(nd-1)*(n-2)+(m-nd)
                print(int(k))
        else:
            if(m<=nd):
                k=2+(n/2-1)*(n/2+2)+(m-1)*(n-2)
                print(int(k))
            else:
                k=2+(n/2-1)*(n/2+2)+(nd-1)*(n-2)+(m-nd)
                print(int(k))