#include <bits/stdc++.h>
using namespace std;
const int maxm = 2001, maxn = 1e5 + 2;
int m;
int st[maxn], ft[maxn], lazy[4 * maxn], T = -1, a[maxn];
vector<int> adj[maxn], P;
bitset<maxm> tmp, seg[4 * maxn];
void add(int id, int L, int R, int l, int r, int &x, bool build);
void dfs(int v, int p = -1) {
  st[v] = ++T;
  for (auto u : adj[v]) {
    if (u != p) dfs(u, v);
  }
  ft[v] = T;
}
inline void relax(int &id, int &L, int &R) {
  if (!lazy[id]) return;
  int x = lazy[id];
  lazy[id] = 0;
  int mid = (L + R) / 2;
  add(2 * id + 0, L, mid, L, mid, x, 0);
  add(2 * id + 1, mid, R, mid, R, x, 0);
}
void add(int id, int L, int R, int l, int r, int &x, bool build) {
  if (R <= l || r <= L) return;
  if (L >= l && R <= r) {
    seg[id] = ((seg[id] << x) | ((seg[id] << x) >> m)) & tmp;
    if (build) {
      seg[id][x] = 1;
    }
    lazy[id] += x;
    lazy[id] %= m;
    return;
  }
  int mid = (L + R) / 2;
  relax(id, L, R);
  add(2 * id + 0, L, mid, l, r, x, build);
  add(2 * id + 1, mid, R, l, r, x, build);
  seg[id] = seg[2 * id] | seg[2 * id + 1];
}
bitset<maxm> get(int id, int L, int R, int l, int r) {
  if (R <= l || r <= L) return 0;
  if (L >= l && R <= r) return seg[id];
  int mid = (L + R) / 2;
  relax(id, L, R);
  return get(2 * id, L, mid, l, r) | get(2 * id + 1, mid, R, l, r);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n >> m;
  for (int i = 0; i < m; i++) tmp[i] = 1;
  for (int i = 2; i < m; i++) {
    bool f = true;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        f = false;
        break;
      }
    }
    if (f) P.push_back(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] %= m;
  }
  for (int i = 0; i < n - 1; i++) {
    int v, u;
    cin >> v >> u;
    v--;
    u--;
    adj[v].push_back(u);
    adj[u].push_back(v);
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    add(1, 0, n, st[i], st[i] + 1, a[i], 1);
  }
  int q;
  cin >> q;
  while (q--) {
    int tp;
    cin >> tp;
    if (tp == 1) {
      int v, x;
      cin >> v >> x;
      v--;
      x %= m;
      add(1, 0, n, st[v], ft[v] + 1, x, 0);
    } else {
      int v;
      cin >> v;
      v--;
      bitset<maxm> B = get(1, 0, n, st[v], ft[v] + 1);
      int ans = 0;
      for (auto p : P)
        if (B[p]) ans++;
      cout << ans << '\n';
    }
  }
}
