Help with wlist problem

#include<bits/stdc++.h>
using namespace std;
int j;
int fun(int n,int a1,int b,int c,int ar){
int k=0,s;
ar[j]=n;
int a;
map<int,int> st;
while(k!=1){
if(j%3==0){
a=a1;
s=n%a;
}
else if(j%3==1){
a=b;
s=n%a;
}
else{
a=c;
s=n%a;
}
s=s
10;
if(s>=a){
j++;
s=s/a;
if( j%3==1 && st.find(s)!=st.end() ){
k=1;
ar[j]=s;
auto itr=st.find(s);
return (itr->second);
}
if(j%3==1){
st.insert(make_pair(s,j));
}
ar[j]=s;
n=ar[j];
}
else{
s=n/a;
while(s>10){
s=s/10;
}
j++;
if(j%3==1 && st.find(s)!=st.end()){
k=1;
ar[j]=s;
auto itr=st.find(s);
return (itr->second);
}
if(j%3==1){
st.insert(make_pair(s,j));
}
ar[j]=s;
n=ar[j];
}
}
}
int main(){
int t,a,b,c,n,d,q;
cin>>t;
for(int tc=0;tc<t;tc++){
cin>>n;
cin>>a>>b>>c;
cin>>q;
j=0;
int ar[100000];
int x=fun(n,a,b,c,ar);
a=ar[x];
b=ar[x+1];
c=ar[x+2];
for(int i=0;i<q;i++){
cin>>d;
if(d==0){
cout<<ar[0]<<endl;
}
else if(d<j){
cout<<ar[d]<<endl;
}
else{
if(d%3==0){
cout<<c<<endl;
}
else if(d%3==1){
cout<<a<<endl;
}
else{
cout<<b<<endl;
}
}
}
}
return 0;
}

This is my code for wlist problem… and it is giving me wrong answer . can anyone tell why.