/**
 *    author:  tourist
 *    created: 21.10.2026 19:29:00
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tt;
  cin >> tt;
  while (tt--) {
    int n, q;
    cin >> n >> q;
    vector<array<int64_t, 4>> qs(n + q);
    for(int i = 0; i < n; i++) {
      int t, x, y;
      cin >> t >> x >> y;
      qs[i] = {t, 1, x, y};
    }
    for(int i = 0; i < q; i++) {
      int t;
      cin >> t;
      qs[n + i] = {t, 2, -1, -1};
    }
    sort(qs.begin(), qs.end());
    vector<array<int64_t, 3>> c;
    map<int64_t, int> mp;
    int ans = 0;
    for (auto [t, op, x, y] : qs) {
      while (!mp.empty() && mp.begin()->first <= t) {
        ans += mp.begin()->second;
        mp.erase(mp.begin());
      }
      if (op == 1) {
        c.push_back({x, y, t});
        for (int i = 0; i < int(c.size()) - 1; i++) {
          auto [xi, yi, ti] = c[i];
          int64_t d = (xi - x) * (xi - x) + (yi - y) * (yi - y);
          auto dt = t - ti;
          if (d == dt * dt) {
            ans += 1;
          }
          if (d > dt * dt) {
            auto ds = int64_t(sqrt(d)) + 3;
            while (ds * ds > d) {
              ds -= 1;
            }
            if (ds * ds == d && (ds - dt) % 2 == 0) {
              mp[t + (ds - dt) / 2] += 1;
              mp[t + (ds - dt) / 2 + 1] += 1;
            } else {
              mp[t + (ds - dt) / 2 + 1] += 2;
            }
          }
        }
      } else {
        cout << ans << '\n';
      }
    }
  }
  return 0;
}
