#!/bin/python3 import sys import itertools n = int(input().strip()) genes = input().strip().split(' ') health = list(map(int, input().strip().split(' '))) s = int(input().strip()) low=0 high=0 for a0 in range(s): ene=0 first,last,d = input().strip().split(' ') first,last,d = [int(first),int(last),str(d)] for i in range(first,last+1): l=len(genes[i]) #print(l) #l=len(ln) for j in range(0,len(d)-l+1): name=d[j:j+l] #print(name) if name==genes[i]: ene+=health[i] if ene>high: high=ene if ene