#include <bits/stdc++.h>
#define bug(x) cout<<#x<<'='<<x<<'\n'
#define tc() int tc;cin>>tc;for(int _tc=0;_tc<tc;++_tc)
#define up(i,l,r) for(int i=l;i<=r;++i)
#define down(i,r,l) for(int i=r;i>=l;--i)
#define fw(file) freopen(file,"w",stdout)
#define fr(file) freopen(file,"r",stdin)
#define reset(x) memset(x,0,sizeof(x))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;

string s;

int main()
{
    ios_base::sync_with_stdio(0);
    vector<pair<string, double> > a;
//    fr("in.INP");
//    fw("out.OUT");
    tc()
    {
        double res=0;
        int c, n;
        a.clear();
        cin >> c >> n;
        a.pb({"JD",1.0});
        up(i,1,c)
        {
            double t;
            cin >> s >> t;
            a.pb({s,t});
        }
        sort(a.begin(), a.end());
        up(i,1,n)
        {
            double t;
            cin >> t >> s;
            //bug(t);bug(s);
            res += t * (lower_bound(a.begin(), a.end(), mp(s,0.0))->se);
        }
        cout << fixed << setprecision(6) << res << '\n';
    }

}
