#include <bits/stdc++.h>
using namespace std;
struct mang {
  int x, y, z;
  mang() {}
  mang(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
  bool operator<(const mang &t) const { return z < t.z; }
} e[100005];
int cntcolor[100005], cntq[100005], isbig[100005];
int last[100005], a[100005], vis[100005], inq[100005], res[100005],
    cntbigq[505][505];
bool in[100005];
pair<int, int> q[100005];
vector<int> bigq[505][505];
vector<pair<int, int> > smallq[100005];
int cnte, itest, cntver;
struct Edge {
  int x, nxt;
} lk[100005];
void addedge(int x, int y) {
  lk[cnte].x = y, lk[cnte].nxt = last[x], last[x] = cnte++;
  lk[cnte].x = x, lk[cnte].nxt = last[y], last[y] = cnte++;
}
void dfs(int x) {
  inq[cntver++] = x;
  vis[x] = itest;
  for (int i = last[x]; i != -1; i = lk[i].nxt)
    if (!vis[lk[i].x]) dfs(lk[i].x);
}
void input() {
  int n, ne, nq, x, y, z;
  int i, j, k, h, lbig;
  scanf("%d %d", &n, &ne);
  for ((i) = 0; (i) < (ne); (i)++) {
    scanf("%d %d %d", &x, &y, &z);
    e[i] = mang(x, y, z);
    cntcolor[z]++;
  }
  sort(e, e + ne);
  scanf("%d", &nq);
  for ((i) = 0; (i) < (nq); (i)++) {
    scanf("%d %d", &x, &y);
    q[i] = make_pair(x, y);
    cntq[x]++, cntq[y]++;
  }
  lbig = 0;
  for ((i) = (1); (i) <= (n); (i)++)
    if (cntq[i] >= 400) isbig[i] = ++lbig;
  for ((i) = 0; (i) < (nq); (i)++) {
    x = q[i].first, y = q[i].second;
    if (isbig[y]) swap(x, y);
    if (isbig[y]) {
      x = isbig[x], y = isbig[y];
      bigq[x][y].push_back(i);
      bigq[y][x].push_back(i);
    } else
      smallq[y].push_back(make_pair(x, i));
  }
  memset(last, -1, sizeof(last));
  for (i = 0; i < ne;) {
    int r = i + cntcolor[e[i].z];
    int dem = 0;
    for (j = i; j < r; j++) {
      if (!in[e[j].x]) in[e[j].x] = true, a[dem++] = e[j].x;
      if (!in[e[j].y]) in[e[j].y] = true, a[dem++] = e[j].y;
    }
    cnte = 0;
    for (j = i; j < r; j++) addedge(e[j].x, e[j].y);
    for ((j) = 0; (j) < (dem); (j)++)
      if (!vis[a[j]]) {
        cntver = 0;
        itest++;
        dfs(a[j]);
        vector<int> bigver;
        for ((k) = 0; (k) < (cntver); (k)++) {
          if (isbig[inq[k]])
            bigver.push_back(isbig[inq[k]]);
          else {
            x = inq[k];
            for (h = 0; h < smallq[x].size(); h++)
              if (vis[smallq[x][h].first] == itest) res[smallq[x][h].second]++;
          }
        }
        for (k = 0; k < bigver.size(); k++) {
          x = bigver[k];
          for (h = k + 1; h < bigver.size(); h++) {
            y = bigver[h];
            cntbigq[x][y]++;
            cntbigq[y][x]++;
          }
        }
      }
    for ((j) = 0; (j) < (dem); (j)++)
      vis[a[j]] = 0, in[a[j]] = false, last[a[j]] = -1;
    i = r;
  }
  for ((i) = (1); (i) <= (lbig); (i)++)
    for ((j) = (i + 1); (j) <= (lbig); (j)++)
      if (bigq[i][j].size())
        for ((k) = 0; (k) < (bigq[i][j].size()); (k)++)
          res[bigq[i][j][k]] = cntbigq[i][j];
  for ((i) = 0; (i) < (nq); (i)++) printf("%d\n", res[i]);
}
int main() {
  input();
  return 0;
}
