Help me in solving MAX_DIFF problem

My issue

for k in range(int(input())):
a,b=map(int,input().split())
l=
for i in range(0,a+1):
for j in range(0,a+1):
if(i+j==b):
l.append(i-j)
print(max(l))

this code is giving correct ouput, but when it comes to submission, it is showing that time limit exceeded.

My code


for k in range(int(input())):
    a,b=map(int,input().split())
    l=[]
    for i in range(0,a+1):
        for j in range(0,a+1):
            if(i+j==b):
                l.append(i-j)
    print(max(l))
                

Learning course: Level up from 1* to 2*
Problem Link: CodeChef: Practical coding for everyone

@jahnaviponagan
Plzz refer the following solution .

# cook your dish here
a = int(input())
for i in range(a):
  n, s = map(int, input().split())
  if s <= n:
    print(s)
  else:
    x = n
    y = s-n
    t = x-y
    print(t)