#include <bits/stdc++.h>
using namespace std;
template <class T, class L>
bool smax(T& x, L y) {
  return x < y ? (x = y, 1) : 0;
}
template <class T, class L>
bool smin(T& x, L y) {
  return x > y ? (x = y, 1) : 0;
}
const int maxn = 1e5 + 17, maxd = 1017;
int n, tmp[maxn], mod, tim, st[maxn], en[maxn], sina[maxn << 2], a[maxn];
vector<int> g[maxn];
bitset<maxd> iman[maxn << 2], prs, ans;
void dfs_order(int v = 0, int p = -1) {
  st[v] = tim;
  a[tim++] = tmp[v];
  for (auto u : g[v])
    if (u != p) dfs_order(u, v);
  en[v] = tim;
}
void DeemoLovesInit() {
  cin >> n >> mod;
  for (int i = 0; i < n; i++) cin >> tmp[i], tmp[i] %= mod;
  for (int i = 1, v, u; i < n; i++)
    cin >> v >> u, g[--v].push_back(--u), g[u].push_back(v);
  dfs_order();
  prs.set();
  for (int i = 2; i < mod; i++)
    if (prs[i])
      for (int j = i * i; j < mod; j += i) prs[j] = 0;
  for (int i = mod; i < maxd; i++) prs.reset(i);
  prs.reset(0), prs.reset(1);
}
void majid(int l = 0, int r = n, int id = 1) {
  if (r - l < 2) {
    iman[id].set(a[l]);
    return;
  }
  int mid = l + r >> 1;
  majid(l, mid, id << 1), majid(mid, r, id << 1 | 1);
  iman[id] = iman[id << 1] | iman[id << 1 | 1];
}
void saddas(int id) {
  (sina[id << 1] += sina[id]) %= mod, (sina[id << 1 | 1] += sina[id]) %= mod;
  iman[id << 1] =
      (iman[id << 1] << sina[id]) | (iman[id << 1] >> mod - sina[id]);
  iman[id << 1 | 1] =
      (iman[id << 1 | 1] << sina[id]) | (iman[id << 1 | 1] >> mod - sina[id]);
  sina[id] = 0;
}
void arpa(int st, int en, int v, int l = 0, int r = n, int id = 1) {
  if (st <= l && r <= en) {
    (sina[id] += v) %= mod;
    iman[id] = (iman[id] << v) | (iman[id] >> mod - v);
    return;
  }
  if (en <= l || r <= st) return;
  saddas(id);
  int mid = l + r >> 1;
  arpa(st, en, v, l, mid, id << 1), arpa(st, en, v, mid, r, id << 1 | 1);
  iman[id] = iman[id << 1] | iman[id << 1 | 1];
}
void hamid(int st, int en, int l = 0, int r = n, int id = 1) {
  if (st <= l && r <= en) {
    ans |= iman[id];
    return;
  }
  if (en <= l || r <= st) return;
  saddas(id);
  int mid = l + r >> 1;
  hamid(st, en, l, mid, id << 1), hamid(st, en, mid, r, id << 1 | 1);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  DeemoLovesInit();
  majid();
  int nq;
  cin >> nq;
  for (int t, v, x; nq--;) {
    cin >> t >> v, v--;
    if (t == 1)
      cin >> x, arpa(st[v], en[v], x % mod);
    else
      ans.reset(), hamid(st[v], en[v]), cout << (ans & prs).count() << '\n';
  }
  return 0;
}
