#include <bits/stdc++.h>
using namespace std;
long long int size[100001], arr[100001], b[100001], lazy[4 * 100001], n, m,
    ind[100001], prime[100001];
vector<long long int> o, v[100001];
bitset<1001> tree[4 * 100001];
bitset<1001> empty, all, pr;
void dfs(long long int cur, long long int par) {
  o.push_back(cur);
  size[cur] = 1;
  for (long long int i = 0; i < v[cur].size(); i++) {
    long long int x = v[cur][i];
    if (x == par) continue;
    dfs(x, cur);
    size[cur] += size[x];
  }
}
void seive() {
  for (int i = 2; i <= 1000; i++) {
    if (!prime[i]) {
      pr.set(i);
      for (int j = 2 * i; j <= 1000; j += i) prime[j] = 1;
    }
  }
}
void build(long long int node, long long int a, long long int b) {
  if (a == b) {
    tree[node].set(arr[a]);
    return;
  }
  long long int mid = (a + b) / 2;
  build(2 * node, a, mid);
  build(2 * node + 1, mid + 1, b);
  tree[node] = tree[2 * node] | tree[2 * node + 1];
}
void update(long long int node, long long int a, long long int b,
            long long int l, long long int r, long long int val) {
  if (lazy[node]) {
    lazy[node] %= m;
    tree[node] =
        ((tree[node] << lazy[node]) & all) | (tree[node] >> (m - lazy[node]));
    if (a != b) {
      lazy[2 * node] = (lazy[2 * node] + lazy[node]) % m;
      lazy[2 * node + 1] = (lazy[2 * node + 1] + lazy[node]) % m;
    }
    lazy[node] = 0;
  }
  if (a > b || a > r || b < l) return;
  if (a >= l && b <= r) {
    tree[node] = ((tree[node] << val) & all) | (tree[node] >> (m - val));
    if (a != b) {
      lazy[2 * node] = (lazy[2 * node] + val) % m;
      lazy[2 * node + 1] = (lazy[2 * node + 1] + val) % m;
    }
    return;
  }
  long long int mid = (a + b) / 2;
  update(2 * node, a, mid, l, r, val);
  update(2 * node + 1, mid + 1, b, l, r, val);
  tree[node] = tree[2 * node] | tree[2 * node + 1];
}
bitset<1001> query(long long int node, long long int a, long long int b,
                   long long int l, long long int r) {
  if (lazy[node]) {
    lazy[node] %= m;
    tree[node] =
        ((tree[node] << lazy[node]) & all) | (tree[node] >> (m - lazy[node]));
    if (a != b) {
      lazy[2 * node] = (lazy[2 * node] + lazy[node]) % m;
      lazy[2 * node + 1] = (lazy[2 * node + 1] + lazy[node]) % m;
    }
    lazy[node] = 0;
  }
  if (a > b || a > r || b < l) return empty;
  if (a >= l && b <= r) return tree[node];
  long long int mid = (a + b) / 2;
  return query(2 * node, a, mid, l, r) | query(2 * node + 1, mid + 1, b, l, r);
}
int main() {
  ios::sync_with_stdio(false);
  ;
  cin.tie(0);
  ;
  seive();
  long long int i, j, x, y, q, type, val, node;
  cin >> n >> m;
  for (i = 0; i < m; i++) all.set(i);
  empty.reset();
  for (i = 1; i <= n; i++) cin >> b[i];
  for (i = 1; i <= n - 1; i++) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  o.push_back(-1);
  dfs(1, -1);
  for (i = 1; i <= n; i++) ind[o[i]] = i;
  for (i = 1; i <= n; i++) arr[i] = b[o[i]] % m;
  build(1, 1, n);
  cin >> q;
  while (q--) {
    cin >> type;
    if (type == 1) {
      cin >> node >> val;
      val %= m;
      update(1, 1, n, ind[node], ind[node] + size[node] - 1, val);
    } else {
      cin >> node;
      cout << (query(1, 1, n, ind[node], ind[node] + size[node] - 1) & pr)
                  .count()
           << "\n";
      ;
    }
  }
  return 0;
}
