#include <bits/stdc++.h>
using namespace std;
template <class T, class X>
inline bool checkbit(T a, X pos) {
  T t = 1;
  return ((a & (t << pos)) > 0);
}
template <class T, class X>
inline T setbit(T a, X pos) {
  T t = 1;
  return (a | (t << pos));
}
template <class T, class X>
inline T resetbit(T a, X pos) {
  T t = 1;
  return (a & (~(t << pos)));
}
template <class T, class X>
inline T togglebit(T a, X pos) {
  T t = 1;
  return (a ^ (t << pos));
}
template <typename T>
T POW(T base, T power) {
  T ret = 1;
  while (power) {
    if (power & 1) ret = (ret * base);
    base = (base * base);
    power >>= 1;
  }
  return ret;
}
template <typename T>
T PPOW(T B, T P) {
  if (P == 0) return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return ((POW(B, P / 2)) * (POW(B, P / 2)));
}
template <typename T>
T Bigmod(T base, T power, T mod) {
  T ret = 1;
  while (power) {
    if (power & 1) ret = (ret * base) % mod;
    base = (base * base) % mod;
    power >>= 1;
  }
  return ret;
}
template <typename T>
T ModInverse(T number, T mod) {
  return Bigmod(number, mod - 2, mod);
}
template <typename T>
T GCD(T a, T b) {
  if (a < 0) return GCD(-a, b);
  if (b < 0) return GCD(a, -b);
  return (b == 0) ? a : GCD(b, a % b);
}
template <typename T>
T LCM(T a, T b) {
  if (a < 0) return LCM(-a, b);
  if (b < 0) return LCM(a, -b);
  return a * (b / GCD(a, b));
}
template <typename T>
T EUCLIDE(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
template <typename T>
T ABS(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}
template <typename T>
double DIS(T x1, T y1, T x2, T y2) {
  return sqrt((double)((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
}
template <typename T>
T ANGLE(T x1, T y1, T x2, T y2) {
  return atan(double(y1 - y2) / double(x1 - x2));
}
template <typename T>
long long isLeft(T a, T b, T c) {
  return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y);
}
void P_ARR(int *ar, int a, int b) {
  if (a > b) swap(a, b);
  if (a <= b) cout << ar[a];
  for (int i = a + 1; i <= b; i++) cout << " " << ar[i];
  cout << endl;
}
template <typename T>
T In() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
bitset<1001> tree[400007];
int ar[400007];
int st[400007], ed[400007];
vector<int> adj[400007];
int id[400007];
int cl;
void push_up(int idx, int l, int r) { tree[idx] = (tree[l] | tree[r]); }
void build(int idx, int s, int e) {
  tree[idx].reset();
  if (s == e) {
    tree[idx].set(ar[id[s]]);
    return;
  }
  int mid = (s + e) >> 1, l = (idx << 1), r = (l | 1);
  build(l, s, mid);
  build(r, mid + 1, e);
  tree[idx] = (tree[l] | tree[r]);
}
int lazy[400007];
int md;
void Rotate(int idx, int v) {
  tree[idx] = ((tree[idx] << v) | (tree[idx] >> (md - v)));
}
void push_down(int idx, int l, int r) {
  Rotate(l, lazy[idx]);
  Rotate(r, lazy[idx]);
  lazy[l] = (lazy[l] + lazy[idx]) % md;
  lazy[r] = (lazy[r] + lazy[idx]) % md;
  lazy[idx] = 0;
}
void update(int idx, int s, int e, int x, int y, int v) {
  if (s == x && e == y) {
    lazy[idx] = (lazy[idx] + v) % md;
    Rotate(idx, v);
    return;
  }
  int mid = (s + e) >> 1, l = (idx << 1), r = (l | 1);
  if (lazy[idx]) push_down(idx, l, r);
  if (y <= mid)
    update(l, s, mid, x, y, v);
  else if (x > mid)
    update(r, mid + 1, e, x, y, v);
  else {
    update(l, s, mid, x, mid, v);
    update(r, mid + 1, e, mid + 1, y, v);
  }
  push_up(idx, l, r);
}
bitset<1001> ret;
void query(int idx, int s, int e, int x, int y) {
  if (s == x && e == y) {
    ret = (ret | tree[idx]);
    return;
  }
  int mid = (s + e) >> 1, l = (idx << 1), r = (l | 1);
  if (lazy[idx]) push_down(idx, l, r);
  if (y <= mid)
    query(l, s, mid, x, y);
  else if (x > mid)
    query(r, mid + 1, e, x, y);
  else {
    query(l, s, mid, x, mid);
    query(r, mid + 1, e, mid + 1, y);
    return;
  }
}
void dfs(int u, int papa) {
  st[u] = ++cl;
  id[cl] = u;
  for (int i = 0; i < (int)adj[u].size(); i++) {
    int v = adj[u][i];
    if (v == papa) continue;
    dfs(v, u);
  }
  ed[u] = cl;
}
bitset<1001> prime, prm;
void sieve() {
  prm.set();
  for (int i = 2; i < md; i++) {
    if (prm[i])
      for (int j = (i + i); j < md; j += i) {
        prm.reset(j);
      }
  }
  prime.reset();
  for (int i = 2; i <= md - 1; i++)
    if (prm[i]) prime.set(i);
}
int main() {
  int n;
  scanf("%d", &(n));
  ;
  scanf("%d", &(md));
  for (int i = 1; i <= n; i++) {
    scanf("%d", &(ar[i]));
    ar[i] %= md;
  }
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    scanf("%d%d", &(x), &(y));
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs(1, 0);
  build(1, 1, n);
  sieve();
  int q;
  scanf("%d", &(q));
  ;
  for (int qq = 1; qq <= q; qq++) {
    int cmd;
    scanf("%d", &(cmd));
    ;
    if (cmd == 1) {
      int x, v;
      scanf("%d%d", &(x), &(v));
      v %= md;
      update(1, 1, n, st[x], ed[x], v);
    } else {
      int x;
      scanf("%d", &(x));
      ;
      ret.reset();
      query(1, 1, n, st[x], ed[x]);
      int cnt = (ret & prime).count();
      printf("%d\n", cnt);
    }
  }
  return 0;
}
