Time limit exceeded in 1 test case

I have tried 2 different methods but in both of them 1 test case is not passing. It is showing timelimit. Please help

1st Approach:
test = int(input())
for i in range(test):
a,b=input().split()
a = int(a)
b = int(b)
for j in range(a,b + 1):
if j == 0 or j == 1:
continue
flag = 0
for k in range(2, int(j**0.5) + 1):
if j % k == 0:
flag = 1
break
if flag == 0:
print(j, end=’ ')
print()

2nd Approach:
t = int(input())
for i in range(t):
n = -1
a,b = input().split()
a = int(a)
b = int(b)
if a >= b:
n = a
a,b=b,a
else:
n = b
l = []
for i in range(n + 1):
l.append(True)
for i in range(2, n + 1):
if l[i] == True:
for j in range(i*i, n + 1, i):
l[j] = False
for i in range(2, n + 1):
if l[i] == True and i >= a:
print(i, end = ’ ')
print()

test = int(input()) is throwing eof error due to which it is generating time limit exceed error.
know try to find out solution for that its your task
ERROR: EOFError: EOF when reading a line
leading to Time limit exceed
if not able to find solution plz tell me i will provide u with solution

I tried to find out the solution to resolve EOFError but couldn’t find it. So please provide me with the solution

to optimsie code and too remove time limit exceed error.