#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int M = 1005;
bitset<M> b[N * 4];
int c[N * 4];
vector<int> prime;
vector<int> e[N];
int n, m, P, tot;
int L[N], R[N], v[N], a[N];
void dfs(int x, int fa) {
  v[tot] = a[x];
  L[x] = tot++;
  for (int y : e[x])
    if (y != fa) dfs(y, x);
  R[x] = tot;
}
void build(int t, int l, int r) {
  if (l + 1 >= r) {
    b[t][v[l]] = 1;
    return;
  }
  int mid = l + r >> 1;
  build(t << 1, l, mid);
  build(t << 1 | 1, mid, r);
  b[t] = b[t << 1] | b[t << 1 | 1];
}
void apply(int t, int x) {
  c[t] = (c[t] + x) % P;
  b[t] = (b[t] << x) | (b[t] >> (P - x));
}
void down(int t) {
  int &x = c[t];
  apply(t << 1, x);
  apply(t << 1 | 1, x);
  x = 0;
}
void add(int t, int l, int r, int ll, int rr, int x) {
  if (l == ll && r == rr) {
    apply(t, x);
    return;
  }
  if (c[t]) down(t);
  int mid = l + r >> 1;
  if (rr <= mid)
    add(t << 1, l, mid, ll, rr, x);
  else if (ll >= mid)
    add(t << 1 | 1, mid, r, ll, rr, x);
  else {
    add(t << 1, l, mid, ll, mid, x);
    add(t << 1 | 1, mid, r, mid, rr, x);
  }
  b[t] = b[t << 1] | b[t << 1 | 1];
}
bitset<M> query(int t, int l, int r, int ll, int rr) {
  if (l == ll && r == rr) return b[t];
  if (c[t]) down(t);
  int mid = l + r >> 1;
  if (rr <= mid) return query(t << 1, l, mid, ll, rr);
  if (ll >= mid) return query(t << 1 | 1, mid, r, ll, rr);
  return query(t << 1, l, mid, ll, mid) | query(t << 1 | 1, mid, r, mid, rr);
}
int main() {
  for (int i = 2; i <= 1000; ++i) {
    bool flag = true;
    for (int j = 2; j * j <= i; ++j)
      if (i % j == 0) flag = false;
    if (flag) prime.push_back(i);
  }
  scanf("%d%d", &n, &P);
  for (int i = 0; i < n; ++i) scanf("%d", a + i), a[i] %= P;
  for (int i = 1; i < n; ++i) {
    int x, y;
    scanf("%d%d", &x, &y), --x, --y;
    e[x].push_back(y), e[y].push_back(x);
  }
  dfs(0, -1);
  build(1, 0, n);
  int q, type, x, y;
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d", &type, &x), --x;
    if (type == 1) {
      scanf("%d", &y);
      add(1, 0, n, L[x], R[x], y % P);
    } else {
      bitset<M> res = query(1, 0, n, L[x], R[x]);
      int ans = 0;
      for (int i : prime)
        if (i < P) ans += res[i];
      printf("%d\n", ans);
    }
  }
  return 0;
}
