class Solution(object):
def evaluate(self, s, knowledge):
"""
:type s: str
:type knowledge: List[List[str]]
:rtype: str
"""
d = {k: v for k, v in knowledge}
n = len(s)
ans = ""
p = 0
while p < n:
if s[p] == "(":
p += 1
t = ""
while s[p] != ")":
t += s[p]
p += 1
p += 1
if t in d:
ans += d[t]
else:
ans += "?"
else:
ans += s[p]
p += 1
return ans