#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int INF = 1E9;
const int MAXN = 100500;
const int MAXK = 500;
int n, m, q, tmr, ans;
int a, b, c;
int used[MAXN];
vector<pair<pair<int, int>, int> > edges;
vector<int> nodes[MAXN];
vector<vector<int> > g[MAXN];
vector<int> component[MAXN];
vector<int> colors[MAXN];
vector<int> big;
int precalc[2111][2111];
void dfs(int color, int v) {
  component[color][v] = c;
  used[v] = tmr;
  for (auto w : g[color][v])
    if (used[w] != tmr) {
      dfs(color, w);
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)(m); i++) {
    scanf("%d%d%d", &a, &b, &c);
    a--, b--, c--;
    edges.push_back({{a, b}, c});
    nodes[c].push_back(a);
    nodes[c].push_back(b);
    colors[a].push_back(c);
    colors[b].push_back(c);
  }
  for (int i = 0; i < (int)(m); i++) {
    sort((nodes[i]).begin(), (nodes[i]).end());
    int nn = unique((nodes[i]).begin(), (nodes[i]).end()) - nodes[i].begin();
    nodes[i].resize(nn);
    g[i].resize(nodes[i].size());
    for (int j = 0; j < (int)(nodes[i].size()); j++) g[i][j].clear();
    component[i].resize(nodes[i].size(), 0);
  }
  for (int i = 0; i < (int)(m); i++) {
    c = edges[i].second;
    a = edges[i].first.first;
    b = edges[i].first.second;
    int v =
        lower_bound((nodes[c]).begin(), (nodes[c]).end(), a) - nodes[c].begin();
    int w =
        lower_bound((nodes[c]).begin(), (nodes[c]).end(), b) - nodes[c].begin();
    g[c][v].push_back(w);
    g[c][w].push_back(v);
  }
  for (int i = 0; i < (int)(n); i++) {
    sort((colors[i]).begin(), (colors[i]).end());
    int nn = unique((colors[i]).begin(), (colors[i]).end()) - colors[i].begin();
    colors[i].resize(nn);
  }
  tmr = 1;
  for (int i = 0; i < (int)(m); i++) {
    tmr++;
    c = 1;
    for (int v = 0; v < (int)(nodes[i].size()); v++) {
      if (used[v] != tmr) {
        c++;
        dfs(i, v);
      }
    }
  }
  for (int i = 0; i < (int)(n); i++)
    if (colors[i].size() >= MAXK) big.push_back(i);
  for (int i = 0; i < (int)(m); i++) nodes[i].push_back(INF);
  for (int i = 0; i < (int)big.size(); i++) {
    int v = big[i];
    for (auto c : colors[v]) {
      for (int j = 0; j < (int)big.size(); j++) {
        int w = big[j];
        int vv = lower_bound((nodes[c]).begin(), (nodes[c]).end(), v) -
                 nodes[c].begin();
        int ww = lower_bound((nodes[c]).begin(), (nodes[c]).end(), w) -
                 nodes[c].begin();
        if (nodes[c][ww] != w) continue;
        assert(nodes[c][vv] == v);
        precalc[i][j] += component[c][vv] == component[c][ww];
      }
    }
  }
  scanf("%d", &q);
  for (int i = 0; i < (int)(q); i++) {
    scanf("%d%d", &a, &b);
    a--, b--;
    ans = 0;
    if (colors[a].size() > colors[b].size()) swap(a, b);
    if (colors[a].size() >= MAXK) {
      int posa = lower_bound((big).begin(), (big).end(), a) - big.begin();
      int posb = lower_bound((big).begin(), (big).end(), b) - big.begin();
      assert(big[posa] == a);
      assert(big[posb] == b);
      ans = precalc[posa][posb];
    } else {
      for (auto c : colors[a]) {
        int v = lower_bound((nodes[c]).begin(), (nodes[c]).end(), a) -
                nodes[c].begin();
        int w = lower_bound((nodes[c]).begin(), (nodes[c]).end(), b) -
                nodes[c].begin();
        if (nodes[c][w] != b) continue;
        assert(nodes[c][v] == a);
        ans += component[c][v] == component[c][w];
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
