Please help me in LNGHW

,

I am having problem in Problem Code: LNGHW, in it I am getting WA in some subtask and correct in some but i dont know whats wrong in my approach plz help me
my solution(python code) is

n,q,m=map(int,input().split())
a=[int(s) for s in input().split()]
for p in range(q):
  i,r=map(int,input().split())
  l=[]
  for p in range(n):
    if a[p]%m==r:
      l.append(a[p])
    l.sort()
  if i < len(l)+1:
    b=l[i-1]
    if b in a:
      print(a.index(b)+1)
  else:
    print("-1")