import itertools
from typing import List


class Category:
    def __init__(self, id, rent=None):
        self.id = id
        self.rent = rent


class House:
    def __init__(self, number, category):
        self.number = number
        self.category = category


class PC:
    def __init__(self, category, step):
        self.category = category
        self.step = step


class Path:
    def __init__(self, starting_house_nr, step):
        self.starting_house_nr = starting_house_nr
        self.step = step


class Simulation:
    def __init__(self, houses):
        self.houses = houses


def get_list_of_factors(n):
    factors = []
    for i in range(1,n+1):
        if n % i == 0:
            factors.append(i)
    return factors


def all_possible_paths(houses_set):
    factors = get_list_of_factors(len(houses_set))
    all_paths = [Path(0, 1)]

    for step in factors[1:]:
        for i in range(step):
            all_paths.append(Path(i, step))

    final_paths = []
    # limit them somehow to the categories
    for path in all_paths:
        target_category = houses_set[path.starting_house_nr].category.id
        valid_path = True
        for index in range(path.starting_house_nr, len(houses_set), path.step):
            house = houses_set[index]
            if house.category.id != target_category:
                valid_path = False
                break
        if valid_path:
            final_paths.append(path)

    return final_paths


def all_possible_categories_sets(empty_places, category_types: List[str], houses):
    variations = [x for x in itertools.product(category_types, repeat=empty_places)]
    all_possible_houses = []

    for variation in variations:
        index = 0
        cur_houses = []
        for house in houses:
            if house.category.id == '?':
                cur_houses.append(House(house.number, Category(variation[index], categories[variation[index]].rent)))
                index += 1
            else:
                cur_houses.append(House(house.number, Category(house.category.id, house.category.rent)))
        all_possible_houses.append(cur_houses.copy())

    return all_possible_houses


def max_total_income(_all_houses_sets):
    max_total = 0

    for houses_set in _all_houses_sets:
        # get all possible paths
        _all_valid_paths = all_possible_paths(houses_set)
        # calculate total values
        current_total = 0
        for path in _all_valid_paths:
            rent = houses_set[path.starting_house_nr].category.rent
            if rent is not None:
                current_total += len(houses_set)/path.step * rent

        max_total = max(current_total, max_total)

    return max_total


if __name__ == '__main__':
    houses_count = int(input())
    houses = []
    categories = {}
    houses_string = input()

    categories_count = int(input())

    category_names = []
    for _ in range(categories_count):
        name, cash = input().split()
        category_names.append(name)
        categories[name] = Category(name, int(cash))

    empty_houses_count = 0
    for i in range(houses_count):
        char = houses_string[i]
        if char == '?':
            empty_houses_count += 1
            houses.append(House(i, Category(char)))
        else:
            houses.append(House(i, Category(char, categories[char].rent)))

    all_houses_sets = all_possible_categories_sets(empty_houses_count, category_names, houses)

    print(max_total_income(all_houses_sets))
