Codeforces Nezzar And Lucky Number

I am getting Wa on test file 2 I couldn’t debug it can someone point me out where my code fails.

	#include <iostream>
	using namespace std;
	#include<vector>
	int main(){
		#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("op.txt","w",stdout);
		#endif
	    int tc;
	    cin>>tc;
	    while(tc--){
	        long long n,d;
	        cin>>n>>d;
	        for(long long i=0;i<n;i++) { 
	        	long long p,x=0,m,ans=0;
	        	cin>>p;
	        	m=p;
	        	if(m%d==0) { cout<<"YES\n"; continue; }
	        	while(m!=0){
	        		if(d==m%10 ) { cout<<"YES\n"; goto last;}
	        		m/=10;
	        	}
	        	
	        	while(p-d>0){
	        		p-=d;
	        		if(p%10==0 || p%d==0 || p%10==d) { ans=1; break; }
	        	}
	        	if(ans) cout<<"YES\n";
	        	else cout<<"NO\n";
	        	last:
	        		ans=0;
	        }
	    }

	}

What happens when p=5 and d=3?

the answer is NO and output is as well NO. I think.

your code fails on if p = 77 and d = 6
as 65 + 6 + 6 = 77
your output shows no

you can add a condition at the starting
if p >= d*10 then yes else your code

My bad!

Your code fails for some other cases because the condition you are checking is not sufficient
For example, if P=31/33/35/37/39 and D=2.
Your code returns NO while answer is yes.
You are not checking the non-last digit when p is big enough