nzec error in python

cook your dish here

T=int(input())
for i in range(T):
N, d= raw_input().split("")
N=N
d=int(d)
A=[]
for i in range(len(N)):
A.append(N[i])
A.append(d)
M=max(A)
if(M==d):
C=0
for j in range(len(A)):
if(C==0):
if(A[j]==d):
C=C+1
A.remove(A[j])
S=""
for k in range(len(A)):
S.append(A[k])
N=int(S)
print(N)
else:
C=0
for j in range(len(A)-1):
if(C==0):
if(A[j]>A[j+1]):
C=C+1
A.remove(A[j])
S=""
for k in range(len(A)):
S.append(A[k])
N=int(S)
print(N)