#include using namespace std; const int N = 100010 , M = 1010; const long long inf = 1e16; long long dp[11][M]; int a[N],b[N],k[M],p[M],q[M]; //void solve(int P,int n,int m ) //{ // for (int i = 0; i < 2; ++i) // for (int j = 1; j <=m; ++j) // dp[P][i][j] = inf; // dp[P][0][0] = 0; // // for (int i = 1; i <= n; ++i) // q[i] = p[i]; // for (int i = 1; i <= n; ++i) // p[i] = max (p[i] - P , 0); // // for (int i = 1; i <= n; ++i) // { // int i1 = i & 1 , i2 = i1 ^ 1; // for (int j = 1; j <= m; ++j) // dp[P][i1][j] = inf; // // for (int j = 0; j <= m ; ++j) // { // //if (j - p[i] >= 0) // dp[P][i1][j] = min (dp[P][i1][j] , dp[P][i2][max(j-p[i],0)] + k[i]); // } // // for (int j = m-1; j >= 0; --j) // dp[P][i1][j] = min (dp[P][i1][j] , dp[P][i1][j+1]); // } // // for (int i = 1; i <=n; ++i) // p[i] = q[i]; //} void solve(int P,int n,int m ) { for (int j = 1; j <=m; ++j) dp[P][j] = inf; dp[P][0] = 0; for (int i = 1; i <= n; ++i) q[i] = p[i]; for (int i = 1; i <= n; ++i) p[i] = max (p[i] - P , 0); for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m ; ++j) { //if (j - p[i] >= 0) dp[P][j] = min (dp[P][j] , dp[P][max(j - p[i],0)] + k[i]); } for (int j = m-1; j >= 0; --j) dp[P][j] = min (dp[P][j] , dp[P][j+1]); } for (int i = 1; i <=n; ++i) p[i] = q[i]; } int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); //int tt,cas=0; scanf("%d",&tt); int n,m; while (scanf("%d%d",&n,&m) != EOF) { int MaxA = 0, MaxB = 0; long long ans = 0; for (int i = 1; i <= n; ++i) { scanf("%d%d",&a[i],&b[i]); MaxA = max( MaxA , a[i]); MaxB = max( MaxB , b[i]); } for (int i = 1; i <= m; ++i) scanf("%d%d",&k[i],&p[i]); for (int P = 0; P <= MaxB; ++P) { solve(P,m,MaxA); } for (int i = 1; i <= n; ++i) { ans += dp[b[i]][a[i]]; ans = min( ans , inf ); } if (ans == inf) ans = -1; printf("%I64d\n", ans ); } return 0; }