#include <stdio.h>
#include <algorithm>
#include <string.h>
#define MAX_N 1000
#define MAX_W 1000

using namespace std;

int n;
int dp[1002][40];
int weight[MAX_W+1];
int cost[MAX_N+1];

int knapsack(int i,int w, int cap)
{
	if(i==n+1) return 0; 
	if(dp[i][w]!=-1) return dp[i][w]; 
		int profit1=0,profit2=0;
		if(w+weight[i]<=cap) 
			profit1=cost[i]+knapsack(i+1,w+weight[i],cap);
	profit2=knapsack(i+1,w,cap); 
	dp[i][w]=max(profit1,profit2); 
	return dp[i][w];
}

int main()
{
	int T ;
	int result[MAX_N];
	scanf("%d",&T);
	for(int j=0;j<T;j++)
	{
		int N;
		scanf("%d", &N);
		n = N;
		memset(weight,-1,sizeof(weight));
		memset(cost,-1,sizeof(cost));
		for(int nob=0; nob<N; nob++)
		{
			scanf("%d %d", &cost[nob], &weight[nob]);
		}
		int G, ans = 0;
		scanf("%d", &G);
		int CAP;
		while(G--)
		{
			scanf("%d", &CAP);
			memset(dp,-1,sizeof(dp));
			ans = ans + knapsack(0,0,CAP);
		}
		result[j] = ans;

	}

	for(int i=0;i<T;i++)
	{
		printf("%d\n", result[i]);
	}

}