function solution(n, t, m, p) {
let result = "";
let str = "";
let num = 0;
while (str.length <= t * m) {
const baseStr = num.toString(n);
str += baseStr;
num++;
}
for (let i = 0; i < str.length; i++) {
if (result.length === t) break;
if ((i + 1) % m === p || ((i + 1) % m === 0 && m === p)) {
result += str[i];
}
}
return result.toUpperCase();
}