#include <bits/stdc++.h>
using namespace std;
const int inf = (1 << 29);
const int maxn = 300005;
const int mod = 1000007;
vector<pair<int, int> > color[maxn];
int q1[maxn], q2[maxn], ans[maxn], qq;
int n, m, num[maxn], top = 0, q[maxn], tot = 0, stk[maxn];
vector<int> edge[maxn];
bool vis[maxn];
long long V[maxn];
int N[maxn], C[maxn];
int F[mod];
void ins(int a, int b) {
  long long hsh = 1ll * a * (n + 2) + b;
  int h = hsh % mod;
  for (int p = F[h]; p > 0; p = N[p]) {
    if (V[p] == hsh) {
      return;
    }
  }
  V[++tot] = hsh, C[tot] = 0, N[tot] = F[h], F[h] = tot;
}
void upd(int a, int b) {
  long long hsh = 1ll * a * (n + 2) + b;
  int h = hsh % mod;
  for (int p = F[h]; p > 0; p = N[p]) {
    if (V[p] == hsh) {
      C[p]++;
      return;
    }
  }
}
int calc(int a, int b) {
  long long hsh = 1ll * a * (n + 2) + b;
  int h = hsh % mod;
  for (int p = F[h]; p > 0; p = N[p]) {
    if (V[p] == hsh) {
      return C[p];
    }
  }
  return 0;
}
void DFS(int x) {
  if (vis[x]) {
    return;
  }
  vis[x] = 1;
  stk[++top] = x;
  for (int i = 0; i < edge[x].size(); ++i) {
    DFS(edge[x][i]);
  }
}
void solve1(int c) {
  if (color[c].empty()) {
    return;
  }
  int all = 0;
  for (int i = 0; i < color[c].size(); ++i) {
    num[++all] = color[c][i].first;
    num[++all] = color[c][i].second;
  }
  sort(num + 1, num + 1 + all);
  all = unique(num + 1, num + 1 + all) - num - 1;
  for (int i = 1; i <= all; ++i) {
    edge[num[i]].clear();
    vis[num[i]] = 0;
  }
  for (int i = 0; i < color[c].size(); ++i) {
    int a = color[c][i].first, b = color[c][i].second;
    edge[a].push_back(b), edge[b].push_back(a);
  }
  for (int i = 1; i <= all; ++i) {
    if (!vis[num[i]]) {
      top = 0;
      DFS(num[i]);
      for (int j = 1; j <= top; ++j) {
        for (int k = j + 1; k <= top; ++k) {
          int a = stk[j], b = stk[k];
          if (a > b) {
            swap(a, b);
          }
          upd(a, b);
        }
      }
    }
  }
}
int fa[maxn];
int getf(int x) { return x == fa[x] ? x : fa[x] = getf(fa[x]); }
void solve2(int c) {
  for (int i = 1; i <= n; ++i) {
    fa[i] = i;
  }
  for (int i = 0; i < color[c].size(); ++i) {
    int a = color[c][i].first, b = color[c][i].second;
    a = getf(a);
    b = getf(b);
    if (a != b) {
      fa[a] = b;
    }
  }
  for (int i = 1; i <= qq; ++i) {
    if (getf(q1[i]) == getf(q2[i])) {
      ans[i]++;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    color[i].clear();
  }
  for (int i = 1, a, b, c; i <= m; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    color[c].push_back(make_pair(a, b));
  }
  tot = 0;
  memset(F, 0, sizeof(F));
  memset(N, 0, sizeof(N));
  memset(C, 0, sizeof(C));
  memset(V, 0, sizeof(V));
  scanf("%d", &qq);
  for (int i = 1; i <= qq; ++i) {
    scanf("%d%d", q1 + i, q2 + i);
    ans[i] = 0;
    if (q1[i] > q2[i]) {
      swap(q1[i], q2[i]);
    }
    ins(q1[i], q2[i]);
  }
  int L = (int)sqrt(m);
  if (1ll * L * L < m) {
    L++;
  }
  for (int i = 1; i <= m; ++i) {
    if (color[i].size() <= L) {
      solve1(i);
    } else {
      solve2(i);
    }
  }
  for (int i = 1; i <= qq; ++i) {
    ans[i] += calc(q1[i], q2[i]);
  }
  for (int i = 1; i <= qq; ++i) {
    if (q1[i] == q2[i]) {
      ans[i] = m;
    }
  }
  for (int i = 1; i <= qq; ++i) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
