#include <bits/stdc++.h>
const int N = 200010;
using namespace std;
int E[N], NEXT[N], HEAD[N], e1[N], e2[N], head[N], Next[N], cnt, CNT, Time, TT,
    V[N], wz[N], L[N], R[N], tu, Q, n, m, i, AA, BB, CC, O, p, f, o, j, H[N],
    A[N], B[N], du[N], now, ans, T[N];
long long Hash[10000010];
map<long long, int> mp, MP;
const int MOD = 10000007;
void add(int a, int b, int c, int d) {
  e1[a] = c;
  e2[a] = d;
  Next[a] = head[b];
  head[b] = a;
}
void dfs(int x) {
  T[++Q] = x;
  int now = HEAD[x];
  while (now != 0) {
    if (V[E[now]] != Time) {
      V[E[now]] = Time;
      dfs(E[now]);
    }
    now = NEXT[now];
  }
}
void WORK(long long x) {
  int P = x % MOD;
  while (Hash[P]) {
    P = (P + 1) % MOD;
  }
  Hash[P] = x;
}
void ADD(int a, int b, int c) {
  E[a] = c;
  NEXT[a] = HEAD[b];
  HEAD[b] = a;
}
int Find(long long x) {
  int P = x % MOD;
  while (Hash[P] && Hash[P] != x) {
    P = (P + 1) % MOD;
  }
  return (Hash[P] == x);
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d%d", &AA, &BB, &CC);
    add(++o, CC, AA, BB);
  }
  for (i = 1; i <= m; i++) {
    p = head[i];
    cnt = 0;
    while (p != 0) {
      A[++cnt] = e1[p];
      B[cnt] = e2[p];
      ADD(++O, e1[p], e2[p]);
      ADD(++O, e2[p], e1[p]);
      p = Next[p];
    }
    CNT = 0;
    Time++;
    for (j = 1; j <= cnt; j++) {
      if (V[A[j]] != Time) {
        H[++CNT] = A[j];
        V[A[j]] = Time;
      }
      if (V[B[j]] != Time) {
        H[++CNT] = B[j];
        V[B[j]] = Time;
      }
    }
    Time++;
    for (j = 1; j <= CNT; j++)
      if (V[H[j]] != Time) {
        V[H[j]] = Time;
        L[++tu] = Q + 1;
        dfs(H[j]);
        R[tu] = Q;
      }
    for (j = 1; j <= O; j++) NEXT[j] = 0;
    O = 0;
    for (j = 1; j <= CNT; j++) HEAD[H[j]] = 0;
  }
  CNT = 0;
  for (j = 1; j <= tu; j++)
    for (i = L[j]; i <= R[j]; i++) wz[i] = j;
  for (j = 1; j <= Q; j++) {
    ADD(++O, T[j], j);
    du[T[j]]++;
    WORK((long long)wz[j] * N + T[j]);
  }
  scanf("%d", &TT);
  while (TT--) {
    scanf("%d%d", &AA, &BB);
    if (mp[(long long)AA * 100005 + BB]) {
      printf("%d\n", mp[(long long)AA * 100005 + BB] - 1);
      continue;
    }
    if (du[AA] > du[BB]) swap(AA, BB);
    now = HEAD[AA];
    ans = 0;
    while (now != 0) {
      ans += Find((long long)wz[E[now]] * N + BB);
      now = NEXT[now];
    }
    mp[(long long)AA * 100005 + BB] = ans + 1;
    mp[(long long)BB * 100005 + AA] = ans + 1;
    printf("%d\n", ans);
  }
}
