#include <bits/stdc++.h>
using namespace std;
int read() {
  int x;
  scanf("%d", &x);
  return x;
}
const int N = 412345;
int n, m;
vector<int> v[N];
vector<int> w;
int a[N];
int tin[N], tout[N], tim;
void dfs(int x, int f) {
  w.push_back(x);
  tin[x] = ++tim;
  for (int i = 0; i < v[x].size(); i++) {
    int to = v[x][i];
    if (to != f) {
      dfs(to, x);
    }
  }
  tout[x] = tim;
}
const int M = 1000;
bitset<M> d[N];
int shift[N];
bitset<M> all;
bitset<M> res;
bitset<M> prim;
void built(int l, int r, int id) {
  if (l == r) {
    d[id].set(a[w[l]]);
    return;
  }
  int y = (l + r) >> 1;
  built(l, y, id + id);
  built(y + 1, r, id + id + 1);
  d[id] = d[id + id] | d[id + id + 1];
}
void apply(bitset<M> &b, int x) { b = (((b << x) & all) | (b >> (m - x))); }
void push(int x) {
  if (shift[x]) {
    shift[x + x] += shift[x];
    if (shift[x + x] >= m) shift[x + x] -= m;
    apply(d[x + x], shift[x]);
    shift[x + x + 1] += shift[x];
    if (shift[x + x + 1] >= m) shift[x + x + 1] -= m;
    apply(d[x + x + 1], shift[x]);
    shift[x] = 0;
  }
}
void upd(int l, int r, int id, int ll, int rr, int x) {
  if (ll <= l && r <= rr) {
    apply(d[id], x);
    shift[id] += x;
    if (shift[id] >= m) shift[id] -= m;
    return;
  }
  push(id);
  int y = (l + r) >> 1;
  if (ll <= y) upd(l, y, id + id, ll, rr, x);
  if (rr > y) upd(y + 1, r, id + id + 1, ll, rr, x);
  d[id] = (d[id + id] | d[id + id + 1]);
}
void getlr(int l, int r, int id, int ll, int rr) {
  if (ll <= l && r <= rr) {
    res |= d[id];
    return;
  }
  push(id);
  int y = (l + r) >> 1;
  if (ll <= y) getlr(l, y, id + id, ll, rr);
  if (rr > y) getlr(y + 1, r, id + id + 1, ll, rr);
  d[id] = (d[id + id] | d[id + id + 1]);
}
int main() {
  int i, j, x, y;
  cin >> n >> m;
  for (i = 1; i <= n; i++) a[i] = read(), a[i] %= m;
  for (i = 1; i < n; i++) {
    x = read();
    y = read();
    v[x].push_back(y);
    v[y].push_back(x);
  }
  w.push_back(0);
  dfs(1, 1);
  for (i = 2; i < m; i++) {
    bool ok = 1;
    for (j = 2; j * j <= i; j++)
      if (i % j == 0) ok = 0;
    if (ok) prim.set(i);
  }
  for (i = 0; i < m; i++) all.set(i);
  int t;
  built(1, n, 1);
  t = read();
  for (i = 1; i <= t; i++) {
    y = read();
    x = read();
    if (y == 1) {
      int sh = read();
      sh %= m;
      if (sh == 0) continue;
      upd(1, n, 1, tin[x], tout[x], sh);
    } else {
      res.reset();
      getlr(1, n, 1, tin[x], tout[x]);
      int ans = (res & prim).count();
      printf("%d\n", ans);
    }
  }
}
