from typing import Set
from typing import Tuple
import string


def interpolate_standard(text: str, d: dict, found: Set[Tuple[str, ...]]) -> str:
    """
    Return the string interpolated as many times as needed.

    :param text: string possibly containing an interpolation pattern
    :param d: dictionary
    :param found: variables found so far
    """
    if not isinstance(text, str):
        return text

    variables = tuple(
        sorted(x[1] for x in string.Formatter().parse(text) if x[1] is not None)
    )

    if not variables:
        return text

    if variables in found:
        raise ValueError("Cycle detected while interpolating keys")
    else:
        found.add(variables)

    interpolated = {v: interpolate_standard(d[v], d, found) for v in variables}
    return text.format(**interpolated)