from typing import List
from typing import Any
from typing import Tuple


def pad(x: List[List[Any]],
        pad: int) -> Tuple[List[List[int]], List[List[bool]]]:
    """Pad 2d list x based on max length. Also generate a mask to access valid
        values in the padded list.

        Args:
            x (List[List[Any]]): The 2d list of values to be padded with pad 
                values.
            pad (Any): the value that will be used to pad x.

        Returns:
            Tuple[List[List[int]], List[List[bool]]]: padded x along with its 
                mask 
        """

    max_length = max(len(sample) for sample in x)

    mask = [
        [True] * len(sample) + [False] * (max_length - len(sample))
        for sample in x
    ]

    x = [sample + [pad] * (max_length - len(sample)) for sample in x]

    return x, mask