#include <cstdio>
#include <iostream>
#include <map>
#include <cstring>
using namespace std;
map <int, string> mp;
string v, name;
int x;
int main()
{
int n, p, ans = 0;
cin >> n >> p;
for(int i = 1; i <= n; i++)
{
cin >> name >> x;
mp[x] = name;
}
while(cin >> v)
{
map <int, string>::iterator it;
it = mp.find(v);
if(it != mp.end())
{
ans += (*it -> first);
}
}
printf("%d\n", ans % p);
return 0;
}