#include #define init(x,y) memset(x,y,sizeof(x)) #define MAX 50010 using namespace std; int n, m, i, j, k, t, a, b; int num[MAX],flag[MAX],team[MAX],suv[MAX]; int main() { scanf("%d", &t); while (t--) { int max[2] = { 0 }; int cnt = 0; int sum = 0; int l = 0; init(suv, 0); init(flag, 0); scanf("%d%d", &n, &m); for (int i = 1; i <=n; i++) { scanf("%d%d", team + i, num + i); } for (int i = 0; i < m; i++) { scanf("%d", &j); flag[j]++; } for (int i = n ; i > 0; i--) { cnt += flag[i]; int temp = num[i] + cnt; if (temp < max[!team[i]]) { suv[i] = 1; } if (max[team[i]] < num[i] + cnt) { max[team[i]] = num[i] + cnt; } } for (int i = 1; i <= n; i++) { if (suv[i] == 0) { sum++; } } printf("%d\n", sum); } return 0; }